473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

IsDate() error

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
Nov 21 '05 #1
2 4449

"Oenone" <no***@nowhere.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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?


You've pretty much got it. IsDate is implemented something like this:

function IsDate(s as string) as boolean
try
dim d as DateTime = DateTime.Parse(s)
return true
catch ex as Exception
return false
end try
end function

IsDate generates an exception on an invalid date, but then catches it and
returns false. If you have first chance exceptions turned on in the
debugger you get a break on the exceptions, but they are thrown and handled
in any case.

David
Nov 21 '05 #2
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:e4**************@tk2msftngp13.phx.gbl...
IsDate generates an exception on an invalid date, but then catches it and
returns false. If you have first chance exceptions turned on in the
debugger you get a break on the exceptions, but they are thrown and
handled in any case.


That's interesting -- and slightly annoying. :)

Is IsDate() unusual in that it can generate exceptions that are caught by
the IDE? I've not seen any other functions that do this.

Are there any other ways to check whether a string can be parsed as a date
that don't generate exceptions? I usually like to run my code (especially
when first writing it) with the IDE set to break on exceptions so that I can
more easily diagnose any problems I encounter. Having it break every time it
checks a date string is infuriating...

--

(O)enone
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vonclausowitz | last post by:
Hi All, I use IsDate() to check a field in Outlook tasks to see if it contains a real date. If IsDate(Task.role) then ... else ... endif
2
by: EP | last post by:
I just upgraded a V 6.0 app. to .Net. I keep getting an error from a simple IsDate statement. Basically checking to see if the value of a variable(vValue = "A2957") is a date example - If...
4
by: John A Grandy | last post by:
IsNumeric() and IsDate() are "leftovers" from the VB6 days ... looking forward, perhaps it would be better not to use them .... does anyone have suggestions on replacements ?
1
by: Sebastian Santacroce | last post by:
Hi, I am trying to use IsDate where the application will use a format of Month/day/year however on a system that uses day/month/year then IsDate uses that system format setting when evaluating if...
2
by: z. f. | last post by:
Hi, i see in reflector in inner implementation of the Microsoft.VisualBasic.Information.IsDate function that looks like that: public static bool IsDate(object Expression) { if (Expression !=...
5
by: Jack Russell | last post by:
Why does IsDate("01:01:0_") return true?? Thanks Jack Russell
19
by: MDC | last post by:
Why does this return true: IsDate("ISometimesHateProgrammingMarch2005") Is there another way to verify that this is not a date?? Thanks in advance! MDC
3
by: MT | last post by:
Platform : VB.NET 2003 I have code as below. Question: The 8 seconds only occured on the 1st time the code run. If I click the button again , the interval become smaller than 1 seconds. This...
1
by: nlulla | last post by:
Hi I am trying to get all rows from a datatable where the first column F1 is a date, as this datatable is created of excel, i only want to deal with rows where the first column is having a date in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.