How to implement Visual Basic .NET IsNumeric functionality by using Visual C#

Summary

This step-by-step article describes how to use the Microsoft Visual Basic .NET IsNumeric function in Visual C#. The IsNumeric function returns a Boolean value that indicates whether an expression can be evaluated as a number. The IsNumeric function returns True when the data type of the expression is Short, Integer, Long, Decimal, Single, or Double. It also returns True if the expression is a string that can be successfully converted to a Double.

back to the top

Create the IsNumeric Function

In Visual C#, you can use the Double.TryParse method to obtain functionality that is similar to IsNumeric. Double.TryParse converts the string representation of a number in a specified style and culture-specific format to its double-precision floating point number equivalent. To create the IsNumeric function:
  1. Start Visual Studio. On the File, point to New, and then click
    Project.
  2. In the New Project dialog box, click
    Visual C# under Project Type.

    Note In Visual Studio 2003, click Visual C# Projects under Project Types.
  3. Under Templates, click Console Application, and then click OK. By default, Program.cs is created.
  4. At the end of the Program class, add the following code for the IsNumeric function:
    // IsNumeric Function
    static bool IsNumeric(object Expression)
    {
    // Variable to collect the Return value of the TryParse method.
    bool isNum;

    // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
    double retNum;

    // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
    // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
    isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
    return isNum;
    }
back to the top

Test the Code

To test the IsNumeric function:
  1. Add the following code to the Main method of Program:
    // Test a numeric string. 
    Console.WriteLine("\"123\" is numeric : {0}", IsNumeric("123"));
    // Test a number.
    Console.WriteLine("-5.64 is numeric : {0}", IsNumeric(-5.64));
    // Test infinity.
    Console.WriteLine("-Infinity is numeric : {0}", IsNumeric("-Infinity"));
    // Test a general string.
    Console.WriteLine("\"12ABC\" is numeric : {0}", IsNumeric("12ABC"));
    // Test with null.
    Console.WriteLine("null is numeric : {0}", IsNumeric(null));
    Console.Read();
  2. On the Debug menu, click
    Start to run the code. Verify the results in the console window.
  3. Press ENTER to close the console window.
back to the top

References

For more information about the TryParse function, visit the following Microsoft Developer Network (MSDN) Web site: For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
325961 HOW TO: Call Visual Basic .NET Run-Time Library Members from Visual C# .NET
back to the top
Propriedades

ID do Artigo: 329488 - Última Revisão: 18 de jul de 2008 - Revisão: 1

Comentários