First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« The New Lifescan OneTouch UltraSmart | Main | Where's the Platform? Where do YOU think... »

Some bits of code:.
C# version of IsNumeric( ) :
public static bool IsNumeric(object value){
   try    {
      int i = Convert.ToInt32(value.ToString());
      return true;
   }
      catch (FormatException)    {
      return false;
   }
}
[via The Wagner Blog]

Actually, this doesn't really do what IsNumeric does, as IsNumeric should also return true for floating point numbers.  This is really more of an "IsInt".
 
Also, why write your own?  You can just reference Microsoft.VisualBasic.dll, then do the following:
if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
 
//Do Something
}
The VB one actually performs better, as it doesn't throw an exception for every failed conversion.
[Sean 'Early' Campbell & Scott 'Adopter' Swigart's Radio Weblog]

What's REALLY interesting is what's going on in the Source Code (disassembled) for the VisualBasic.Information.IsNumeric (as I am trepidacious to include this dependancy (dunno why) in my code). 

Looks like it's pretty much the checking the Type of the object via iConvertable or the alternate TryParse...:

public static bool Isumeric (object Expression)
{
bool f;
ufloat64 a;
long l;

IConvertible iConvertible = null;
if ( ((Expression is IConvertible)))
{
   iConvertible = (IConvertible) Expression;
}

if (iConvertible == null)
{
   if ( ((Expression is char[])))
   {
       Expression = new String ((char[]) Expression);
       goto IL_002d; 'hopefully inserted by optimizer
   }
   return 0;
}
IL_002d:
TypeCode typeCode = iConvertible.GetTypeCode ();
if ((typeCode == 18) || (typeCode == 4))
{
    string str = iConvertible.ToString (null);
   try
   {
        if ( (StringType.IsHexOrOctValue (str, l)))
   {
        f = true;
        return f;
   }
}
catch (Exception )
{
    f = false;
    return f;
};
return DoubleType.TryParse (str, a);
}
return Utils.IsNumericTypeCode (typeCode);
}

internal static bool IsNumericType (Type typ)
{
bool f;
TypeCode typeCode;
if ( (typ.IsArray))
{
    return 0;
}
switch (Type.GetTypeCode (typ))
{
case 3:
case 6:
case 7:
case 9:
case 11:
case 13:
case 14:
case 15:
   return 1;
};
return 0;
}

Tracked by:
"IsNumeric in C#" (brownsblogging) [Trackback]


Thursday, March 16, 2006 10:11:29 AM (Pacific Standard Time, UTC-08:00)
if you don't want to include the visual basic namespace, you can write you're own VB helper methods. Here's an example of the IsNumeric method:

public static bool IsNumeric(object expression)
{
if (expression == null)
return false;

double number;
return Double.TryParse(Convert.ToString(expression, CultureInfo.InvariantCulture), System.Globalization.NumberStyles.Any, NumberFormatInfo.InvariantInfo, out number);
}
Comments are closed.

Contact

Sponsors

Hosting By

On this page...

Tags

Calendar

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Google Ads