I'm working on a program that requires the user to input an integer. How do I prevent the user from entering a non-numeric value? I tried using the IsNumeric() function but I get an error before I can use it. I get the error at the console.read, before I can call the IsNumeric() function. Here is my code:
Dim num As Integer
Console.Write("enter num:")
num = Console.ReadLine
If IsNumeric(num) = True Then
Console.WriteLine("valid. num = " & num)
Else
Console.WriteLine("invalid")
End If
Any help is greatly appreciated.
Try this:
Dim num As Integer
Console.Write("enter num:")
Dim input = Console.ReadLine
If Integer.TryParse(input, num) Then
Console.WriteLine("valid. num = " & num)
Else
Console.WriteLine("invalid")
End If