|
-
Feb 27th, 2006, 01:31 PM
#1
Thread Starter
New Member
Using VB 6 functions in .net
Hello everyone!
I am looking for a way to access the vb 6 version of IsDate(). The .net version under microsoft.visualbasic.isdate is just a wrapper for datetime.parse(). Usually the .net version works fine if the data you pass it is already suposed to be a date and you are just verifying it. If the expression passed is not a date an exception is thrown. Exceptions are slow, at least slow enough that I can't run the function back to back 30 plus times without it taking 60 seconds or so, in vb 6 it takes less than a second.
So what I need is a way to use the function that VB6 calls, not the .net wrapper. I could make a COM object with just that function inside but I am trying to make a 100% .net project and using COM would violate that. I know calling VB6 directly IS using COM, but atleast I don't have to worry about installing my cheap little IsDate class.
In the end I am trying to write a class that will go through plain text and pick out any dates, if anyone has any suggestion I am all ears.
-
Feb 27th, 2006, 01:44 PM
#2
Re: Using VB 6 functions in .net
Use regular expressions. That is a .NET way. Regex class can be found in System.Text.RegularExpressions namespace.
You can use this Regular Expression to validate the date
VB Code:
MessageBox.Show(System.Text.RegularExpressions.Regex.IsMatch("12-12-2005", "((([0][1-9]|[12][\d])|[3][01])[-/]([0][13578]|[1][02])[-/][1-9]\d\d\d)|((([0][1-9]|[12][\d])|[3][0])[-/]([0][13456789]|[1][012])[-/][1-9]\d\d\d)|(([0][1-9]|[12][\d])[-/][0][2][-/][1-9]\d([02468][048]|[13579][26]))|(([0][1-9]|[12][0-8])[-/][0][2][-/][1-9]\d\d\d)"))
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 27th, 2006, 01:48 PM
#3
Thread Starter
New Member
Re: Using VB 6 functions in .net
Shuja Ali
I tried that, I found that same snipet of code elsewhere and it didn't work on the following format "April 28, 1992". I understand why it didn't work and I wish I could fix it but I don't know how to write regular expressions.
-
Feb 27th, 2006, 01:50 PM
#4
Re: Using VB 6 functions in .net
soo.... why not re-format it for purposes of the function?
-tg
-
Feb 27th, 2006, 04:04 PM
#5
Thread Starter
New Member
Re: Using VB 6 functions in .net
I don't know how hard it is to write regular expressions and concidering the scope of the project speding a lot of time writing the regular expression may not be worth it compared to having to carry around a COM component.
I was hoping it would be as easy as referencing some VB 6 dll that would contain the VB 6 IsDate function.
-
Feb 27th, 2006, 04:18 PM
#6
Re: Using VB 6 functions in .net
 Originally Posted by NeoGioZ
I don't know how hard it is to write regular expressions and concidering the scope of the project speding a lot of time writing the regular expression may not be worth it compared to having to carry around a COM component.
I was hoping it would be as easy as referencing some VB 6 dll that would contain the VB 6 IsDate function.
I would still suggest either using the Regular Expressions or use the ParseExact method of DateTime Class.
If you use IsDate function, you might have to change it finally down the lane. Because you never when MS will remove VB namespace from .NET.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 27th, 2006, 05:06 PM
#7
Re: Using VB 6 functions in .net
 Originally Posted by NeoGioZ
I don't know how hard it is to write regular expressions and concidering the scope of the project speding a lot of time writing the regular expression may not be worth it compared to having to carry around a COM component.
I was hoping it would be as easy as referencing some VB 6 dll that would contain the VB 6 IsDate function.
Odds are you're not the first person to need this... there's plenty of resources out there (this place being one of them) where you can get a canned regex. I still don't see why you can't format it and pass it to the regex.... but what ever.
-tg
-
Feb 27th, 2006, 05:20 PM
#8
Thread Starter
New Member
Re: Using VB 6 functions in .net
 Originally Posted by techgnome
Odds are you're not the first person to need this... there's plenty of resources out there (this place being one of them) where you can get a canned regex. I still don't see why you can't format it and pass it to the regex.... but what ever.
-tg
I can't preformat because I am reading through plain text, legal jargon to be exact, and out of a whole paragraph of text I need to pick out one or two dates that could be in any format acceptable in U.S English.
-
Feb 27th, 2006, 05:33 PM
#9
Thread Starter
New Member
Re: Using VB 6 functions in .net
 Originally Posted by Shuja Ali
I would still suggest either using the Regular Expressions or use the ParseExact method of DateTime Class.
If you use IsDate function, you might have to change it finally down the lane. Because you never when MS will remove VB namespace from .NET.
Thank you for your replies everyone , man this is a though one...
I can't use ParseExact because it's too slow, I need to run through 30+ words and pick out a date or two out of legal jargon, all that needs to happen in a second or two. My old VB 6 code does it in under a second.
Does anyone know of a good regular expressions tutorial? I might have to give in and learn regular expressions.
on the vb namespace...
Each version of .net is supposed to include the previous version inside it. By design my code can only run in the .net framework and since every version will encompass the previous version I am guaranteed that the vb namespace will always be there. Microsoft even recomends using the vb namespace if you know what your looking for because it save coding time by not having to figure out the .net way.
-
Feb 27th, 2006, 06:41 PM
#10
Re: Using VB 6 functions in .net
You haven't said what version of VB.NET you're using. If it's 2005 then use Date.TryParse.
-
Feb 28th, 2006, 09:20 AM
#11
Thread Starter
New Member
Re: Using VB 6 functions in .net
 Originally Posted by jmcilhinney
You haven't said what version of VB.NET you're using. If it's 2005 then use Date.TryParse.
I am .net 1.1 (vs 2003). Thanks.
-
Feb 28th, 2006, 09:25 AM
#12
Re: Using VB 6 functions in .net
then just simulate the tryparse like this
VB Code:
Dim dt As Date
Try
dt = Date.Parse("23 April Watevaaa...")
Catch
'code for if string was not a valid date
End Try
-
Feb 28th, 2006, 11:01 AM
#13
Thread Starter
New Member
Re: Using VB 6 functions in .net
 Originally Posted by Phill64
then just simulate the tryparse like this
VB Code:
Dim dt As Date
Try
dt = Date.Parse("23 April Watevaaa...")
Catch
'code for if string was not a valid date
End Try
That is the problem, date.parse is very slow in .net 1.1 if the value passed is not a date. Try it with a odd string and see how long it takes to return an exception.
BTW, Date.TryParse in .net 2.0 works great! Now I need to convience my boss to fork out the cash.
-
Feb 28th, 2006, 04:53 PM
#14
Re: Using VB 6 functions in .net
As an aside, Double.TryParse was originally created because IsNumeric had the same issue that you are experiencing with IsDate. IsNumeric was reimplemented to use Double.TryParse, so I'd guess that in .NET 2.0 IsDate has been reimplemented to use Date.TryParse. That still doesn't mean that it's a good idea to use the Runtime functions. You lose nothing if you don't need to keep the parsed value but if you do then you'll end up parsing it twice; once in the IsNumeric or IsDate function to check whether it's valid, then again to actually get the value.
I guess what you could do is use a tool to look inside the Date.TryParse method and then create your own method that works the same way in .NET 1.1. I've never used it but I believe that Reflector is a useful tool for this. You may also like to get some of the other useful tools from that page.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|