Home All Groups Group Topic Archive Search About

Checking a string for valid date

Author
1 Mar 2005 9:13 PM
Bob Day
The IsDate code below should result in False, instead it throws the
exception below.  Why?  How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.



Additional information:  the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.

Author
5 Apr 2005 3:38 AM
Bob Day
Thanks everyone, that all makes perfect sense.

Bob
------
Show quoteHide quote
"Bob Day" <Bob***@TouchTalk.net> wrote in message
news:eU2h1NqHFHA.3244@TK2MSFTNGP09.phx.gbl...
> The IsDate code below should result in False, instead it throws the
> exception below.  Why?  How do I check if a string can be converted to a
> date if this function does not work properly?
>
> Bob
>
> code:
> Dim blnDate_Valid As Boolean = True
> Dim x As String = "Hello"
>
> blnDate_Valid = IsDate(x)
>
> Should result in False, but throws exception:
>
> A first chance exception of the type System.FormatException occurred in
> mscorlib.dll.
>
>
>
> Additional information:  the string was not recognized as a valid
> datetime. There is a unknown word starting at index 0.
>
>
>
>
Author
5 Apr 2005 9:10 AM
Oenone
>> blnDate_Valid = IsDate(x)
>> Should result in False, but throws exception:

Actually, I still think this is an error in VB.NET. I don't understand why
IsDate allows an internally-thrown exception to be caught by the procedure
that calls it. For example, consider the following code:

        Dim s As String
        s = "Blah"
        Debug.WriteLine(IsNumeric(s))
        Debug.WriteLine(IsDate(s))

If you run that with the IDE set to break on CLR exceptions, it successfully
processes the IsNumeric() call without any problems, but then breaks into
the IDE on the IsDate() call. This seems inconsistent to me.

The exception can't even be caught by the calling procedure. For example:

        Try
            Debug.WriteLine(IsDate(s))
        Catch ex As Exception
            Debug.WriteLine("Error: " & ex.Message)
        End Try

Run this with the IDE set NOT to break and you'll find that the exception is
not caught. So there's no reason to allow the IDE to know that an exception
occurred.

IsDate() is the only function in the entire language runtime that I've found
that exhibits this behaviour.

It's also darned annoying. :) I like to run my code with the IDE always set
to break on exceptions as I find it much easier to track problems down if I
can immediately see where the exception occurred. I've had to write a
wrapper around IsDate() that performs some basic validation (not an empty
string, first character is numeric, etc.) before it calls into IsDate in an
attempt to weed out as many non-date values as I can. :-/

Hopefully this will change in VS2005.

--

(O) e n o n e
Author
5 Apr 2005 4:31 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Oenone" <oen***@nowhere.com> schrieb:
>>> blnDate_Valid = IsDate(x)
>>> Should result in False, but throws exception:
>
> Actually, I still think this is an error in VB.NET. I don't understand why
> IsDate allows an internally-thrown exception to be caught by the procedure
> that calls it. For example, consider the following code:
>
>        Dim s As String
>        s = "Blah"
>        Debug.WriteLine(IsNumeric(s))
>        Debug.WriteLine(IsDate(s))
>
> If you run that with the IDE set to break on CLR exceptions, it
> successfully
> processes the IsNumeric() call without any problems, but then breaks into
> the IDE on the IsDate() call. This seems inconsistent to me.

Mhm...  There is nothing special within 'IsDate''s implementation:

\\\
....
If TypeOf Expression Is String Then
    Try
        Dim time1 As DateTime =
DateType.FromString(CType(Expression,String))
        Return True
     Catch exception1 As  Exception
     End Try
End If
....
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
5 Apr 2005 4:51 PM
Oenone
Herfried K. Wagner [MVP] wrote:
> Mhm...  There is nothing special within 'IsDate''s implementation:
[...]

Is the implementation of IsNumeric similar to this?

(And how did you get to the sourcecode for the IsDate function?)

--

(O) e n o n e
Author
5 Apr 2005 4:58 PM
Cor Ligthert
Oenone,

You don't need the source code, it is just a way it is done, this is the
same

try
    dt as datetime = CDate(mydatestring)
catch ex as error
    return error
end try

And a kind of same implementation is for IsNumeric.

Cor
Author
5 Apr 2005 5:05 PM
Oenone
Cor Ligthert wrote:
> You don't need the source code, it is just a way it is done, this is
> the same

I know that, but Herfried was posting from the actual VB IsDate() function.

I've found how to access it myself now (using the DotNet Reflector
application at http://www.aisto.com/roeder/dotnet/). It's very interesting
to see how the functions are working internally. And IsNumeric() is quite a
lot more complex than IsDate()...

--

(O) e n o n e
Author
5 Apr 2005 5:26 PM
Herfried K. Wagner [MVP]
"Oenone" <oen***@nowhere.com> schrieb:
>> Mhm...  There is nothing special within 'IsDate''s implementation:
> [...]
>
> Is the implementation of IsNumeric similar to this?
>
> (And how did you get to the sourcecode for the IsDate function?)

You can check the implementation yourself:

<URL:http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector.zip>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>