468,525 Members | 2,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

Check, if string is convertable to another type

Hello,

maybe this is an easy one:
Is there any way to check, if a string value can be
converted into another type (e.g. DateTime, Integer)
without using exceptions (try/catch)?

Thanks in advance,
Daniel
Nov 15 '05 #1
2 2292
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.VisualBasic, and use the
IsNumeric, IsDate, etc functions defined there. However, some of those have
lots of code in them, and my not actually be more performant than try/catch.

Lastly, you could use a compiled regular expression to match the data, which
(After the first time) will probably be faster than either other method.

-Philip

"Daniel" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hello,

maybe this is an easy one:
Is there any way to check, if a string value can be
converted into another type (e.g. DateTime, Integer)
without using exceptions (try/catch)?

Thanks in advance,
Daniel

Nov 15 '05 #2
Philip Rieck <st***@mckraken.com> wrote:
in c#, not really - although some types do have a .TryParse() function.

If you wish to, you can reference Microsoft.VisualBasic, and use the
IsNumeric, IsDate, etc functions defined there. However, some of those have
lots of code in them, and my not actually be more performant than try/catch.

Lastly, you could use a compiled regular expression to match the data, which
(After the first time) will probably be faster than either other method.


If you're interested in performance and will be getting a lot of "bad
data", you should also consider a first-pass method which might (say)
look at each character in the string to make sure it's appropriate.
I've found that this kind of thing can be far faster at filtering out a
lot of obviously bad candidates and then using an "industrial
strength" technique on the rest than just going straight to the heavy
stuff.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by John Roth | last post: by
reply views Thread by chris | last post: by
9 posts views Thread by Ronald Fischer | last post: by
28 posts views Thread by Davy | last post: by
4 posts views Thread by Gaijinco | last post: by
6 posts views Thread by tommaso.gastaldi | last post: by
2 posts views Thread by rn5a | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.