views:

30

answers:

4

I'm trying to do some validation testing in VB.NET.

 If Entity.WeekEndDate.ToString = String.Empty Then
            ErrorList.Add(New cValidationError("id", "Incorrect Week End Date"))

Where WeekEndDate is of type Date. When I originally build the object, how can I insert a value into WeekEndDate that will generate an empty string(ie. "") when converted from a Date to a String?

A: 
WeekEndDate = Nothing
Ira Rainey
+3  A: 

It might be better to use MinDate to represent an invalid date:

If Entity.WeekEndDate = DateTime.MinDate Then 
     ErrorList.Add(New cValidationError("id", "Incorrect Week End Date"))

As a rule, you shouldn't be converting dates to strings and then comparing them. Compare as the original datatype (in this case, DateTime).

Mitch Wheat
Nullable dates even better?
Nate Zaugg
i believe the out-of-the-box winforms date controls have problems with null dates...
Mitch Wheat
I didn't originally write this validation object, so I was unsure as to what they were trying to check against. Thanks for the input!
A: 

Make it nullable and compare to null instead.

MSDN reference: http://msdn.microsoft.com/en-us/library/bb981315(VS.80).aspx

Peter Forss
A: 

There is no date value that will produce an empty string when calling ToString. If you need this functionality, the column must be nullable in your database and your entity must use the Nullable(Of DateTIme) type for the value.

You can then say:

If Entity.WeekEndDate Is Nothing Then
    ErrorList>Add(new cValidationError("id", "Incorrect Week End Date"))

And to produce the error, just say:

Entity.WeekEndDate = Nothing
Adam Robinson