Hi! Enveryone:
I am new in C#. I want to check whether the user input to a text box is a number or not. What is the string function to check it I do not want to use try and catch.
Thank you very much!
CLC
Hi! Enveryone:
I am new in C#. I want to check whether the user input to a text box is a number or not. What is the string function to check it I do not want to use try and catch.
Thank you very much!
CLC
How to check string is number or not in C#
Danddba
This will not work if the user hits a character that is not a digit but it could be in a number like the dot '.' character or the negative/minus '-' character.
WPF Fan
Within your method, an exception occurs for most cases where the expression is not numeric. You catch the exception within the method. The fact that you don't throw the exception further is not the issue.
Run a test with both methods and you'll see that with relatively few iterations, the one that relies on exceptions is very slow when the expression is not numeric. For example, at fewer than 100 iterations you can see an obvious difference. If you have this "unexceptional exception" approach throughout your code, you will have performance issues.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
iljforever
newdrim
But within the method you are forcing an exception to occur when the expression is not numeric. Forcing exceptions as part of normal logic flow when there is a more efficient alternative is something I wouldn't recommend.
Double.TryParse is available in 1.1 (and 1.0 also).
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
silent tone
If you are using the new .NET Framework 2.0 (C# 2005), then below code will work for you:
oreXero
Yes, you're right! The TryParse method on double is available in framework 1.1.
Vinu_P
bool textIsNumeric = true;
try
{
int.Parse(TextBox1.Text);
}
catch
{
textIsNumeric = false;
}
I know you said you didn't want to use try/catch but this is simple and effective.
Jordan111
In the TextBox_TextChange check the charater by using the following code
for (int i=0; i<this.textbox1.text.length; i++)
{
if (char.IsDigit(this.textbox1.text
)==false)
// give any error
}
Sponchiz
"Would you regard it as exceptional that an expression is passed to this routine that is not numeric "
No, which is exactly why I don't throw the exception. If an exception is caught, the string is obviously NOT numeric, and the method should return false. This is a perfectly acceptable case for not throwing an exception.
Note that the example you posted will only work on .NET framework 2, where the TryParse methods are available.
Holy-Fire
Thanks for all of you.
I tried it and it solve my problem. In .Net 1.1, Double.TryParse use four paremeters so follows:
Double.TryParse(Str, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out Num)
Thank again!
CLC
freeflyr
public static System.Boolean IsNumeric (System.Object Expression)
{
if(Expression == null || Expression is DateTime)
return false;
if(Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
return true;
try
{
if(Expression is string)
Double.Parse(Expression as string);
else
Double.Parse(Expression.ToString());
return true;
} catch {} // just dismiss errors but return false
return false;
}
}
ollertoncj
Exceptions should be just that - exceptions. Would you regard it as exceptional that an expression is passed to this routine that is not numeric
An IsNumeric in C# which does not rely on unexceptional exception logic is:http://www.tangiblesoftwaresolutions.com/Articles/CSharp%20Equivalent%20to%20IsNumeric.htm
Elisabeth
Without using TryParse, however, this is the only way I can see it happening.
Mr. Underhill