Converting an empty string to a date, and then back
I'm trying to do some validation testing in VB.NET.
If Entity.WeekEndDate.ToString = String.Empty Then
ErrorList.Add(New cV开发者_JAVA百科alidationError("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?
It might be better to use MinValue to represent an invalid date:
If Entity.WeekEndDate = DateTime.MinValue 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).
WeekEndDate = Nothing
Make it nullable and compare to null instead.
MSDN reference: http://msdn.microsoft.com/en-us/library/bb981315(VS.80).aspx
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
加载中,请稍侯......
精彩评论