views:

62

answers:

6

I'm reviewing some code in VB.net, and in a validation object they have written the following

If Not IsDate(Entity.SelectedDate) Then
            ErrorList.Add(New CValidationError("MainReport", "Please select a weekend date"))

SelectedDate is of type Date. It seems to me it would be impossible to ever hit this condition. Is this true?

A: 

I don't believe it is possible to ever hit this condition. The SelectedDate can only be set by either the calendar control or the OnSelectionChanged event handler. Even then, the value can only be set to a date or a Null. Maybe this should check to ensure that a value was selected that is Not Null. Otherwise, just ditch the check altogether.

Registered User
That was basically my thought, it seemed like this was just a silly mistake they added to validation, but I thought I should be sure. Thank you for your answer.
A: 

It gonna be never to that error. SelectedDate is of type Date. So you gonna check that this is a date.

SvenVdb
My thoughts exactly, even at a default value (uninstantiated) it will still be of type date.
+1  A: 

IsDate will always return true for any object which can be converted to a date: http://msdn.microsoft.com/en-us/library/00wf8zk9%28VS.80%29.aspx

It will even return true if you do

dim myDate as Date
myDate = Nothing
IsDate(myDate)

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/3f70ef9e-79d0-4c89-ac33-ea16829e8298 says:

Please note that Date is a value type, not reference type, therefore assigning Nothing to a variable of type Date will set it to its default value (see what Lars says).

So IsDate(date) will always return true.

It is worth double checking however that IsDate refers to the standard VB function and isn't some custom function written elsewhere (it seems odd that the validation error text refers to weekend dates, so it might be a custom function). Right-click it and "go to definition" to make sure and see where Visual Studio goes to. If it is the one in the Microsoft.VisualBasic namespace then I would say it is always going to return true.

bgs264
Your second statement is inaccurate. `IsDate(Nothing)` will return false. In the link you provided, the person asking the question wrote `Dim a As Date = Nothing`, which assigns `a` to the **default** value for the `DateTime` type (which I believe is `DateTime.MinValue`); so he didn't actually pass `Nothing` to `IsDate`; he passed the default `DateTime` value.
Dan Tao
@Dan Tao thank you. Fixed in an edit.
bgs264
A: 

It looks like they're checking for a weekend date, so why not check the DateTime.DayOfWeek for saturday or sunday?

MCain
A: 

It is indeed the IsDate from Microsoft.VisualBasic namespace; the weekend date error message is actually asking for the date that the week is ending on, usually a Friday. It seems clear now that this will always return true; thank you everyone for the resources and confirmation.

Hi linkrift, thanks for reporting back. Usually you would post this as a comment on your original question, not as a separate answer.
MarkJ
+1  A: 

Here's what I see in Reflector:

Public Shared Function IsDate(ByVal Expression As Object) As Boolean
    Dim time As DateTime
    If (Expression Is Nothing) Then
        Return False
    End If
    If TypeOf Expression Is DateTime Then
        Return True
    End If
    Dim str As String = TryCast(Expression,String)
    Return ((Not str Is Nothing) AndAlso Conversions.TryParseDate(str, (time)))
End Function

Now, the question is: if passed a Date (VB.NET's keyword for DateTime values), could this method ever return false?

No.

If (Expression Is Nothing)

This will never be true for a boxed value type.

If TypeOf Expression Is DateTime

This will always be true if the method is explicitly passed a Date.

Even if TypeOf A Is B returned false when B is a subclass of A (which it doesn't), you could still assume this would always return true since DateTime, as a value type, cannot be inherited.

So you're good.

My best guess is that this code originally called IsDate on a String or Object that was not strongly typed; at some point, someone must have updated the SelectedDate property to be typed as Date without bothering to update this validation code.

Dan Tao
Great answer, very clear and well organized. Thank you very much!