Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is IsDate function slow? 2

Status
Not open for further replies.

CosmicCharlie

Programmer
Jun 30, 2006
44
US
Has anyone else experienced incredible slowness with the IsDate function in VB.Net? I have found that it can take up to three seconds to return a value, especially if the return value is false. In the samples I am using it is reading a value from either a text box or a treeview node.

IsDate seems like one of those functions that should have been made obsolete by something new in .Net, but the help files suggest no other alternative.

Does anyone have a solution to this?

Thank you!

Cosmic Charlie
 
Hi,

an ugly alternative would be to create your own "IsDate" function that could more or less look like this:

Code:
public function IsDate(Value as Object) as Boolean

     try
        cdate(value)
        return True
     catch ex as exception 'or as invalidcastexception
        return False
     end try

end function

hope it helped somehow
 
Ugly? Well, a little bit.

But I am learning the hard way that users want solutions that are good enough and they want them now. They don't want to wait for elegance. If your solution gets a working product out the door faster, then that is what I will use.

I will still field other ideas though, if anyone has one.

Thank you, Masta!

 
for the text box at least are you ensuring that they at least have to put the date in with some form of formatting?

-The answer to your problem may not be the answer to your question.
 
Qik3Coder,

Thank you for your question. Unfortunately, questions of formatting do not really apply to my situation. In this program, there are several valid entries that users can type into the textbox that are not dates. I need the IsDate function, or something like it, to analyze the data before taking any one of a list of possible actions.

I am trying to find out what other .Net developers' experiences have been with IsDate and what alternatives have been developed for it, if any.
 
Rather than doing the isDate
why don't you do something like

Dim boolIsDate as boolean
try
ctype(myTextBox1.Text,DateTime)
boolIsDate = True
catch
boolIsDate = False
end Try

'do logic based on whether or not date here...

-The answer to your problem may not be the answer to your question.
 
Qik3Coder, please refer to MastaKilla's response.

The suggestion, by the way, works but I am finding that the CType function is no faster than IsDate when applied to dates. This is not consistent however. Presumably IsDate uses this method behind the scenes.

I am still curious if anyone has had experiences similar to mine with IsDate. My question really is about that function.

 
I saw MastaKilla's response, this would be a variation on his answer. By the way, Ctype, from what i have heard should be used over the type specific casts, such as CINT, or CDATE.

If your question is based around why a specific function performs rather than how to deal with a problem then you may want to dig around on the microsoft website and see what you can scrape up.

If you coding solution to whatever problem you are trying to solve allows you enter multiple datatypes into the same input box then you will always have some performance hit, while you try to identify what you were given by the user.

-The answer to your problem may not be the answer to your question.
 
Qik3Coder,

I poked around in the Help files, which confirmed your assertion that CDate is faster than CType. Unfortunately, it is less useful because it will convert integers within the valid range as dates or times, and this can produce the wrong result sometimes.

Regarding your observation about a performance hit, I want to stress that that is not the point. The point is that IsDate, by itself and absent any other surrouding code, is often extremely slow -- usually when returning False. I want to know if any one else has also found IsDate to be slow and if there are any equivalent solutions that are faster. Using CDate comes close, but is not ideal.





 
Note: You should never use a try catch block as a test. It sucks up a lot of horse power every time an exception is created.

BUT... Unfortunately if you need speed out of the IsDate function the Qik3Coder's recommendation might be faster if it is in fact a date, but probably about the same if what it gets is not a date and has to create and handle an exception.

The slowdown inside of IsDate may be coming from both verifying that the object it received is convertible... and then verifying that it is convertible to a Date prior to returning a boolean. If it is not a date, then I suspect that it throws an exception that is internally handled prior to returning false.


I have a feeling that the only real solution for processing speed is to offer the user a choice to enter a Date or something else. In this way you will be able to pre-flag the data and circumvent the slow IsDate Return of False. Maybe offer them a Date/TimePicker control in place of the Textbox on a Enter Date CheckBox.Checked event or something.


Senior Software Developer
 
Thank you, Sirius, for your reply, which directly addresses the issues I raised.

It looks like I am stuck. The fact is that for various reasons specific to this situation, the date/time picker cannot be used and multiple controls cannot be used. I have been back and forth with the users (and their boss) on this one, and I lost the debate, albeit for (mostly) sound business reasons. (Okay, and a few political ones too.)

Anyway, this means that I will have to explain to them that they must accept the performance hit if they get the date wrong ... all the more incentive to get the date right.

Still, I don't recall IsDate ever being this slow in VB 6.0. Do you?





 
Well... I remember everything being slow in VB 6.0! LOL!

Probably just the computers got faster. ;o)


Initially you said that many different things might go into that text box. If you test for a date on every one of those and most are not dates then you will still see that slowdown on all of the non-dates. That’s the Theory anyway. So, if there was any way in which you could prevent running the IsDate function where you don't need to then I would avoid it.

Maybe something like testing the string for "/" or "-" or ":" or "am" or "pm".... well you get the point. LOL! Then if it starts to fit a date mold... then test it with IsDate.


Senior Software Developer
 
Actually, the IsDate test is the last thing I do, for the very reasons you mentioned. Basically, acceptable values in the text box are limited to a few variations on some set values, or a time, or nothing. Anything else is invalid. I do everything possible to avoid having to call IsDate, but it really is not avoidable in all cases. Your pre-testing idea (looking for "am" etc.) is a good idea and I can use that.


 
If you're using VS2005, you could use a maskedtextbox and give it a complex regex mask that would only allow typing in the few variations on some set values, or a time, or nothing. If you're using vs2003, you can use a simple textbox and do the regex validation yourself.

Great regex links:
Regex Tutorial
Regex Library
 
Heya CC,
Well, it's not a pretty solution in the least, but it will likely give you faster responses...

Code:
public function MyIsDate(Value as string) as boolean
 dim bReturn as boolean = false

 dim sParts() as string = Value.split("/")
 if sParts.lengh = 3 then
   dim iMonth as integer = integer.tryparse(sParts(0)) 
   if iMonth > 0 and iMonth <= 12 then
     dim iDay as integer = integer.tryparse(sParts(1)) 
     if iDay > 0 and iMonth <=31 then
       dim iYear as Integer = integer.tryparse(sParts(3))
       if (iYear > 0 and iYear < 100) OR (iYear > 1900 and iYear < 2025) then
         bReturn=True
     end if
   end if
 end if

 return bReturn
end function

You might want to add some extra code to validate the day value in months with less than 31 days, but even then, a handful of integer compares is going to take a lot less time than a Try/Catch that fails.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thank you both, TRG and MK! These give me a lot of options that, at the very least, will reduce calls to IsDate to a minimum. And now would be a good time to learn about Regex.

CC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top