Microsoft KB Archive/329488

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

Article ID: 329488

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q329488



IN THIS TASK

 * SUMMARY
 * Create the IsNumeric Function
 * Test the Code
 * REFERENCES



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:  Start Visual Studio .NET or Visual C# 2005. On the File, point to New, and then click Project. In the New Project dialog box, click Visual C# Projects under Project Type.

Note In Visual Studio 2005, click Visual C# under Project Types. Under Templates, click Console Application, and then click OK. By default, Class1.cs is created.  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:   Add the following code to the Sub Main method of Class1: // Test a numeric string. Console.WriteLine(&quot;\&quot;123\&quot; is numeric : {0}&quot;, IsNumeric(&quot;123&quot;)); // Test a number. Console.WriteLine(&quot;-5.64 is numeric : {0}&quot;, IsNumeric(-5.64)); // Test infinity. Console.WriteLine(&quot;-Infinity is numeric : {0}&quot;, IsNumeric(&quot;-Infinity&quot;)); // Test a general string. Console.WriteLine(&quot;\&quot;12ABC\&quot; is numeric : {0}&quot;, IsNumeric(&quot;12ABC&quot;)); // Test with null. Console.WriteLine(&quot;null is numeric : {0}&quot;, IsNumeric(null)); Console.Read;                 On the Debug menu, click Start to run the code. Verify the results in the console window.</li> Press ENTER to close the console window.</li></ol>

back to the top

<div class="references_section">