How to implement Visual Basic .NET IsNumeric functionality by using Visual C# .NET or Visual C# 2005 (329488)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# 2005, Express Edition

This article was previously published under Q329488

SUMMARY

This step-by-step article describes how to use the Microsoft Visual Basic .NET IsNumeric function in Visual C# .NET or Visual C# 2005. 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# .NET or in Visual C# 2005, 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 .NET or Visual C# 2005. On the File, point to New, and then click Project.
  2. In the New Project dialog box, click Visual C# Projects under Project Type.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  3. Under Templates, click Console Application, and then click OK. By default, Class1.cs is created.
  4. At the end of the Class1 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 Sub Main method of Class1:
    // 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

Modification Type:MajorLast Reviewed:1/19/2006
Keywords:kbString kbConsole kbHOWTOmaster KB329488 kbAudDeveloper