Wednesday, August 24, 2011

To check for numeric strings

in VB, I think isNumeric should do the trick.

for C#, one way is to:

try
{
val = Convert.ToInt32(textBox2.Text.ToString().Trim());
}
catch (Exception e)
{
//not a valid number
}

another method, slightly more troublesome, but still does the trick.

double res;
string a;
a = "#$%^&";
if (double.TryParse(a, out res)) { } else { MessageBox.Show("a is not a number"); }
a = "12345678";
if (double.TryParse(a, out res)) { MessageBox.Show("a is a number!!!"); }
a = "1234a5678";
if (double.TryParse(a, out res)) { } else { MessageBox.Show("a is not a number....."); }

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home