468,525 Members | 2,251 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,

This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or IsNumeric to do
this, but how can i do it in C#?

Thanks in advance,
Daniel
Nov 15 '05 #1
2 6928

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

This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or IsNumeric to do
this, but how can i do it in C#?

Thanks in advance,
Daniel


There is no such thing as IsNumeric in C#. You'll have to work with a try
catch to test it or use the VB function.

public static bool IsNumeric(string input)
{
try
{
int.Parse(input);
return true;
}
catch
{
return false;
}
}

Yves
Nov 15 '05 #2
Decimal has a TryParse function that won't throw an exception.

C# can reference the VB assemblies and call IsNumeric (shudder) -- please
don't do this. It's been a while since I looked at the code for IsNumeric,
but I think, in addition to doing some additional processing (VB is
sometimes more lenient on conversions), it might do a Try/Catch internally.

Try/Catch is usually how it's done, unless it's a performance critical
section, in which case you might run it through a regex first (remember to
take into account culture settings).

-mike
MVP

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

This is maybe a simple one:
Is there any way to check, if a string can be converted to
another type (like DateTime or Integer) without using
try/catch? In VB, i can use IsDate or IsNumeric to do
this, but how can i do it in C#?

Thanks in advance,
Daniel

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
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.