???? ID: 329488 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

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

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

This step-by-step article describes how to use the Microsoft Visual Basic .NETIsNumericfunction in Visual C#. TheIsNumericfunction returns a Boolean value that indicates whether an expression can be evaluated as a number. TheIsNumericfunction returnsTrue?? ???? ?????? ?? ?????????? ?? ???, ????????, ????, ?????, ???, ?? ????? ??? ?? ?? ???? ??True??? ?????????? ?? ?? ???????? ?? ?? ???? ??????????? ?????? ???? ?? ??? ??? ????? ???

IsNumeric ?????? ?????

??? ????? C#, ????? ?? ???? ???Double.TryParse??????????? ?? ???? ???? ?? ?? ??????? ???? ?? ??? ????IsNumeric.Double.TryParse???? ?? ??????? ????????? ????? ?????? ?????? ???????? ???????? ?? ??? ?????? ????????? ???? ?? culture-??????? ?????? ??? ?????? ???? ??? ????? ?? ???IsNumeric?????? ?? ??? ???? ???.:
  1. Visual Studio ??????? ????? ????? ?????????? ????? ????,????? ????-????? ????, ?? ???? ????????????.
  2. ???????? ?????????????? ????? ???, ????? ????????? C#?? ???????????????? ??????.

    ???:Visual Studio 2003, ????? ????????? C# ??????????? ???????????????? ??????.
  3. ?? ???????????????????? ????,????? ??????????? ????-????? ????, ?? ???? ???OK. ???????? ??? ??, Program.cs ?? ???? ???
  4. ?? ????????????????, ?? ??? ????? ??? ??????IsNumeric?????? ?? ??? ???? ???.:
    // 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;
    }		

??? ?? ??????? ????

??????? ???? ?? ???IsNumeric?????? ?? ??? ???? ???.:
  1. ????? ??? ???????????????????? ?? ????:
    // 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. ????? ???????? ???????? ??,?????????? ?? ?????? ?????? ????? ??? ?????? ?? ?????? ?????
  3. ?????? ????? ?? ??? ???? ?? ??? ENTER ??????

??????

?? ???? ??? ???? ??????? ?? ???TryParse?????, ????? Microsoft ?????? ??????? (MSDN) ?? ???? ??? ????:
Double.TryParse ????
(VS.71) http://MSDN.Microsoft.com/en-us/library/SYSTEM.double.tryparse .aspx (http://msdn.microsoft.com/en-us/library/system.double.tryparse(VS.71).aspx)
???????? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ????? ????::
325961  (http://support.microsoft.com/kb/325961/EN-US/ ) TO HOW: ??? Visual Basic .NET ??-???? ????????? ??????? ?? ????? C# .NET

???? ???? ???? ??:
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# 2008 Express Edition
??????: 
kbstring kbconsole kbhowtomaster kbmt KB329488 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:329488  (http://support.microsoft.com/kb/329488/en-us/ )