I'm having a couple of problems with the IsDate function in VB.net (.net
framework v1.1). Having searched the web and usenet for others having the
same problem, I've seen several posts from people reporting the same error
as me, followed by various responses from others saying they can't recreate
the problem.
Well I think I know how to recreate it, but I still don't understand why
it's occurring. Does anyone have any ideas?
Try the following code:
Dim t As Object = DBNull.Value
If IsDate(t.ToString) Then
MsgBox("Computer says yes")
Else
MsgBox("Computer says no")
End If
If you run this with Common Language Runtime Exceptions set to Continue, it
works just fine and the computer says no. However, if you switch it to Break
on CLR Exceptions, when the IsDate() is executed the following exception is
thrown:
A first chance exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: String was not recognized as a valid DateTime.
Clicking Continue produces a second exception:
A first chance exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Date' is not valid.
....clicking Continue again results in the computer saying no. If you wrap a
Try/Catch statement around the code, the Catch block does not get called.
This is very confusing to me, I've not seen VB.net act in this way before.
It seems that the IsDate() function raises an exception but then somehow
still handles it and carries on regardless. Even when CLR Exceptions are set
to Continue, there's still the tell-tale delay that occurs when an exception
is thrown when running in the IDE, even though the program carries on
without error.
Can anyone shed any light on this for me?
--
(O)enone