-1

Ok, so I really have no idea how to do this, but how would I prevent a crash when it asks for an integer value and the user gives it a string?

Here's some example code;

private sub userInput()
    dim userInputNum As Integer
    userInputNum = InputBox("Enter a number.")
end sub

And if the user enters text, it just crashes. So I was wondering if anyone had a solution?

Thanks, I would appreciate it a lot.

Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
Blair Cross
  • 13
  • 1
  • 2

1 Answers1

3

Use Integer.TryParse

Dim userInputNum As Integer

Dim noInput = Console.ReadLine

If Integer.TryParse(noInput, userInputNum) Then
    Console.WriteLine("Integer accepted")
Else
    Console.WriteLine("Please don't enter non integer values")
End If
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65