Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
VB
//for string

if (carName.Length == 0)
returnMessage += Car Name cannot be blank!


//how you check if is empty for dateTime
Posted
Comments
Matt T Heffron 30-Jan-14 13:26pm    
The question doesn't really make sense.
A DateTime always represents some instant in time with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.).
If you mean the string representation of a DateTime that you will Parse, then just check the string as above.
ZurdoDev 30-Jan-14 13:36pm    
You should post as solution. I think OP just wants to know how to tell if DateTime has been specified since it cant' be empty string.
Sergey Alexandrovich Kryukov 30-Jan-14 13:45pm    
In such cases, I usually suggest a different idea, which I think is better, because dealing with some "unset" time is a source of some uncertainty. Please see Solution 1.
—SA
ZurdoDev 30-Jan-14 13:46pm    
True. As long as you have control over the declaration of it and it isn't in a library.
Sergey Alexandrovich Kryukov 30-Jan-14 14:25pm    
Right.
—SA

In addition to the valuable advice given to you by Sergey, I'd like to add some comments on 'DateTime.

1. DateTime is a Struct: it behaves like a Value Type: no "reference" to it is possible. So, it can never be null.

2. An un-initialized DateTime has the default value of DateTime.MinValue: "1/1/0001 12:00:00 AM," and its TimeOfDay Property the default value: "00:00:00"

3. Programmers do often use comparing DateTime.MinValue with a DateTime (non-nullable) to test for an invalid, or missing, date.

4. When you use a nullable DateTime, some interesting things happen: consider these declarations
C#
DateTime? dt1;
DateTime dt2;
DateTime dt3 = new DateTime(0);
DateTime? dt4 = DateTime.MinValue;
DateTime? dt5 = new DateTime(2014, 1, 31, 6, 0, 0, DateTimeKind.Utc);
Consider:
C#
bool isMinDate1 = dt1 == DateTime.MinValue; // false
bool isMinDate2 = dt2 == DateTime.MinValue; // true
bool isMinDate3 = dt3 == DateTime.MinValue; // true
bool isMinDate4 = dt4 == DateTime.MinValue; // true
bool isMinDate5 = dt5 == DateTime.MinValue; // false
If you try and access DateTime Fields, like TimeOfDay, in a nullable DateTime: you will get an error, even if the nullable DateTime contains a valid DateTime instance, not null !

You'll note that if you display the IntelliSense for a nullable DateTime you get only the basic Object properties, but you do get a 'Value Field. For a non-nullable DateTime, there is no 'Value Field.

However, there's a "gotcha" in using the 'Value Field: if the nullable DateTime variable is un-initialized, like 'dt1 in the code above, your app will break with a System.InvalidOperationException error if you try and access it.

So, make your choice based on consideration of these differences in "regular" vs. nullable DateTime Types.

And, keep in mind that a DateTime? is really a Nullable<DateTime> ... see: [^]. Nullable<T> generic Types were implemented in .NET 2.0.

You might want to review Value vs. Reference Types in .NET: [^].
 
Share this answer
 
v2
On Top of Bill and SA solution.
try this too. might help you.

Nullable DateTime
C#
// Handling with Nullable  DateTime
            DateTime? dateTimeNullable = null;  // null values can be assigned
            if (dateTimeNullable.HasValue)       //use HasValue property to check it has value or not
                return dateTimeNullable.Value;


Non Nullable DateTime
C#
//Handling DateTime object without Nullable
DateTime dateTime = DateTime.MinValue;  // null values cannot be assigned since it as value type, u can assign min value or default value
if ( dateTime != DateTime.MinValue)  // for value type you can check with min value or default value
    return dateTimeNullable;
 
Share this answer
 
Comments
BillWoodruff 30-Jan-14 19:02pm    
+5 The 'HasValue field of nullable DateTime is a very vital piece of information in this context ! I have wondered why the implementation of 'Value in nullable DateTime does not return null if there is "no value," but I suspect that's an artifact of DateTime? really being a short-hand for Nullable<datetime>
Matt gave you some idea in your comment, but, if really need to clearly indicate the "no-date" condition (or, better say, "no time"), you should use slightly different type. Instead of System.DateTime, use nullable System.DateTime?. It would give you some different dimension:
C#
System.DateTime? time = //...

//...

if (time != null) {
   SystemDateTime myTime = time.Value;
   //...
} else
   DoSomethingElse();


I hope you got the idea. If not, read about nullable data types and on how to make value types nullable.

—SA
 
Share this answer
 
v2
Comments
CPallini 30-Jan-14 13:52pm    
5. However you should actually use the nullable type in your code. :-)
Sergey Alexandrovich Kryukov 30-Jan-14 14:24pm    
Thank you, Carlo.
Is is that "However"?
—SA
CPallini 30-Jan-14 14:35pm    
shouldn't be
System.DateTime? time = //...
?
Sergey Alexandrovich Kryukov 30-Jan-14 17:31pm    
Ah, sure, thank you very much.
—SA
Rahul VB 30-Jan-14 14:36pm    
ctrl + shift + d very nice Sir, by the way how have you been?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900