tayspen
<Insert title here>
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99
Or just put that code into a try/catch block and output the error message or a custom message
That's what I would do. If you're converting astring to a number type, the Convert.ToInt32() method (or any of the other methods in the Convert class) will throw an exception if the string doesn't really contain a number.
Here's something I whipped up really quick as an example. It should work, but I'm still kind of new at this C# business:
string inputString;
try
{
Convert.ToInt32(inputString);
}
catch (System.FormatException)
{
Console.Writeline("Please input a number in inputString");
}
...That's pretty simple, but it shows you how to use error checking.
alc6379
Cookie... That's it
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
In that case this is how I would do it like this.
private static void IsNumeric(string input)
{
try
{
Convert.ToInt32(input);
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
Then if you wanted to test if it was a number or not.
IsNumeric("4");
In that case no error would be thrown. Where as if you did this.
IsNumeric("F");
An error would be thrown.
Again, its the almost the same code as alc's its just a little different way to handle the error :).
tayspen
<Insert title here>
1,622 posts since Jul 2005
Reputation Points: 84
Solved Threads: 99