Tutorial :Is there any way to get IsDate(Date) to come back as false?



Question:

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?


Solution:1

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.


Solution:2

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.


Solution:3

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.


Solution:4

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


Solution:5

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


Solution:6

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »