2020 - Validation for entering a numerical value in a range
The Issue
When entering some numerical values at the keyboard, very few are checked to see if they are in a valid range. Remember these values will not cause an exception when converting them, but they are out of the ranges required.
The Solution
We can create an if statement to check if the value entered is within range. We could also use a while loop so that the value can be re-entered.
Example
while (true)
{
Console.Write("Enter X coordinate for new outlet: ");
x = Convert.ToInt32(Console.ReadLine());
if (x > 0 && x <50)
{
break;
}
catch
{
Console.WriteLine("invalid data");
}
}