Greets,
There are a couple of ways to do this. The first is by using the
Convert.ToInt32() method as you pointed out. There is also the Parse()
method of the Int32 class which can be used to accomplish the same thing.
In either case, you will need to check for an exception if it cannot be
parsed.
Another alternative is to use the RegEx class in the
System.Text.RegularExpressions namespace in order to match against a regex
pattern. Below is an example which uses RegEx to perform the validation:
// required using for RegEx class
using System.Text.RegularExpressions;
public bool IsInteger(string data)
{
return Regex.IsMatch(test, @"^[-+]?[0-9]\d*\.?[0]*$");
}
A good link which has a library of regular expressions patterns which
can be used with the RegEx class is the regex library:
http://www.regexlib.com/
They have several useful patterns which can be used to validate e-mail,
dates, time, numbers, etc.
Regards,
Joe
David Krussow said:
I need to test to see if a string value can be converted to an integer. I
thought I'd try Convert.ToInt32(strTemp) within a Try Catch block. Is there
a better way to accomplish this?
Thanks.