isnumeric in c sharp

  • Thread starter Thread starter crowl
  • Start date Start date
C

crowl

I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

Thanks for all assistance.
 
Hi,

You can attempt to feed the textbox's Text property value to the Parse
method of the Int32 type, and catch the FormatException, OverflowException
and optionally ArgumentNullException (this one is unlikely to be raised).
If any of the exceptions has been thrown, the textbox's text is not a valid
integer.

This approach works the same way for Double, Decimal or any other type
supporting the Parse method.
 
Is there a reason you can't use a try / catch block with System.Convert

such as

void Button_Click(object sender, eventargs e

// verify the textbox contains an intege
tr

int i = System.Convert.ToInt32(TextBox1.Text, 10)
// if you get here then the i contains the integer value of TextBox1.Tex

catch (System.FormatException e

// the conversion failed so use the default value
i = defaultValue

// Continue processing


Jackson Davis [msft
-
This posting is provided "AS IS" with no warranties, and confers no right
Samples are subject to the terms specified a
http://www.microsoft.com/info/cpyright.ht
 
Hello Jackson,

I think catching FormatException only would be an unreliable solution.
Imagine the user has typed something like "9999999999999999999999999999" to
the textbox. In this case, I suppose an OverflowException instance will be
thrown upon calling the ToInt32 method, not a FormatException.
 
crowl said:
I have a pocket pc project in c#.

From a textbox i have to verify if the input is numeric. I have found
in the msdn a sample like this: textbox1.numeric = true / false. But
this do not work in my environment.

If I convert the textbox content explicit to a numeric value, and is
is not numeric, the program ran into the error handler (try catch).
This is not what I want!

For example: 2 textbox, 1 button
If the user click the button, the program should proof if the content
of the textboxes is numeric, if not, it should use a standard value
and then go on.

A nice method like 'isnumeric' would be glad.

You can use VB.NET's IsNumeric method. Add a reference to
Microsoft.VisualBasic.dll, then call the static method:

Microsoft.VisualBasic.Information.IsNumeric( object)
 
Here is my adaptation to the IL of the VB "IsNumeric" method. Of course,
this is not as exhausting. There is a lot of IL I can't make sense of and a
lot of Locale stuff I didn't even attempt. Assuming you using ASCII
characters in the string and all others, numerics, this should work.


---------------------------------------------------

using System;

namespace CSharpInformation
{

public class Information
{
public static bool IsNumeric(object expression) {

IConvertible convertible = null;
TypeCode typecode;
string text;
double num;
bool result = false;
char chr;

if ((expression as IConvertible) != null) {
convertible = (IConvertible)expression;
}

if (convertible == null) {
if ((expression as char[]) != null) {
expression = new string((char[])expression);
}
else {
return false;
}
}

typecode = convertible.GetTypeCode();

if (typecode == TypeCode.String || typecode == TypeCode.Char) {
text = convertible.ToString(null);

try {
for (int i=0; i<text.Length; i++) {
chr = Convert.ToChar(text.Substring(i, 1));

if (char.IsNumber(chr)) {
result = true;
}
else if ((int)chr >= 'a' && (int)chr <= 'f') {
result = true;
}
else if ((int)chr >= 'A' && (int)chr <= 'F') {
result = true;
}
else {
result = false;
}

if (result == false) {
break;
}
}
}
catch(Exception) {
return false;
}

if (result == false) {
result = double.TryParse(text,
System.Globalization.NumberStyles.Any, null, out num);
}
}

if (result == false) {
result = IsNumericTypeCode(typecode);
}

return result;
}

internal static bool IsNumericTypeCode(TypeCode typeCode) {
bool result = false;

switch (typeCode) {
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
result = true;
break;

default:
result = false;
break;
}

return result;
}
}
}
 
public static bool IsNumeric(object value)
{
try
{
double d = System.Double.Parse(value.ToString(),
System.Globalization.NumberStyles.Any);
return true;
}
catch (FormatException)
{
return false;
}
}
 
Here is some code that doesn't rely on error trapping:

public static bool IsNumeric(string stringToTest)

{

double newVal;

return double.TryParse(stringToTest, NumberStyles.Any,
NumberFormatInfo.InvariantInfo, out newVal);

}
 
At the least, I have to admit that the double.TryParse works well and is a 1
liner. Looking at the VB IsNumeric clone I posted above, it is much
simpler.

Thanks,
Shawn
 
Back
Top