• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

C# Equivalent to VB's IsNumeric Function

Instant C# converts calls to the legacy VB IsNumeric function via the following helper class inserted into the conversion output:

//----------------------------------------------------------------------------------------
// Copyright © 2003 - 2017 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// The methods in this class replicate the behavior of IsNumeric and Val.
//----------------------------------------------------------------------------------------
using System;
using System.Globalization;

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

        double testDouble;
        if (expression is string)
        {
            CultureInfo provider;
            if (((string)expression).StartsWith("$"))
                provider = new CultureInfo("en-US");
            else
                provider = CultureInfo.InvariantCulture;

            if (double.TryParse((string)expression, NumberStyles.Any, provider, out testDouble))
                return true;
        }
        else
        {
            if (double.TryParse(expression.ToString(), out testDouble))
                return true;
        }

        //VB's 'IsNumeric' returns true for any boolean value:
        bool testBool;
        if (bool.TryParse(expression.ToString(), out testBool))
            return true;

        return false;
    }
}


Use Instant C# to convert from VB.NET to C#.     

Additional resource: VB.NET and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.