Is there any IsNumeric in C#?

S

Steve Kershaw

Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(string)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve
 
J

Juan T. Llibre

Import Microsoft.VisualBasic and use it!

if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
//Do Something
}

if (Microsoft.VisualBasic.Information.IsNumeric("yadda"))
{
//Do Something
}



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
G

Guest

Steve,

Try this:

public static bool IsInteger(string theValue)
{
try
{
Convert.ToInt32(theValue);
return true;
}
catch
{
return false;
}
}
}
 
J

Juan T. Llibre

From Scott Hanselman's blog at :

http://www.hanselman.com/blog/ExploringIsNumericForC.aspx

Actually, this doesn't really do what IsNumeric does,
as IsNumeric should also return true for floating point numbers.

That code does not return true for floating point numbers,
which -nevertheless- *are* numbers.

That is really more of an "IsInt".

Importing Microsoft.VisualBasic does the complete job.




Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
K

Kevin Spencer

In the .Net 2.0 CLR, there is an Int32.TryParse() method that doesn't throw
an exception, and you can use that. In .Net 1.1, you're pretty much stuck
with the alternatives that the others have already proposed.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
S

Steve C. Orr [MVP, MCSD]

There is no equivalent for this function in C#. VB.NET has lots of
functions that C# developers have to create manually.
Or, you can import the visual basic namespace and use its IsNumeric function
from within C#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top