IsNumeric() in c#.net?

G

Guest

Hello, friends,

In VB6, we have function IsNumeric(). What is the correpsonding function in
c#.net?

Thanks.
 
M

mnumzane

Hello, friends,

In VB6, we have function IsNumeric(). What is the correpsonding function in
c#.net?

Thanks.

This is what I've found when searching through www.google.com/codesearch,
i've put it in a class for easy access throughout the application:


/// <summary>
/// Returns whether an object is numeric or not
/// </summary>
/// <param name="o">object to check</param>
/// <returns>boolean true/false</returns>
public static bool IsNumeric(object o)
{
if (o is IConvertible)
{
TypeCode tc = ((IConvertible)o).GetTypeCode();
if (TypeCode.Char <= tc && tc <= TypeCode.Decimal)
return true;
}
return false;
}
 
J

Jon Skeet [C# MVP]

This is what I've found when searching throughwww.google.com/codesearch,
i've put it in a class for easy access throughout the application:

That's not what's being asked for in this case, I believe. For
instance, it returns false for "5". It's not a case of asking whether
the type is a numeric type, so much as whether a particular object can
be converted into a number without error.

Jon
 

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