I started a course in C# last night. I am trying to program a calculator for a console application. What I am trying to do is test that a value passed isNumeric
The variable is...
string input1;
To convert this to a number I'm using...
number1 = double.Parse(input1);
But I want to test that it's a number before I parse it, I can test for an empty value...
if (input1 == "")
{
Console.WriteLine("Invalid entry.");
goto myLabel_1;
}
But if I try
if (Char.IsNumber(input1)), I get a cannot convert from 'string' to 'char' error.
Any suggestions on how to check that a value is not an alpha character?
Thanks in advance
Anthony TrudeauPosted Oct 12, 2007, 12:25 PM
AlanPosted Oct 9, 2007, 10:19 AM
If you're using .NET 2.0 or greater, you can use the double.TryParse() method:
string input1 = Console.ReadLine();
double number1;
bool isNumeric = double.TryParse(input1, out number1);
if (isNumeric)
{
// do something with number1
}
else
{
Console.WriteLine("Invalid entry.");
}