Search code examples
vb.netif-statementisnumeric

Whats going wrong with my IF statement and Is Numeric


The following code reveals a gap in my understanding. Can someone please tell me what it is and how to fix the code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim NumberToGuess, Answer As Integer
    NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
    While Answer <> NumberToGuess
        Answer = InputBox("Please enter your guess")
        If IsNumeric(Answer) = False Then MsgBox("That ain't no number")
        If Answer > NumberToGuess Then MsgBox("Too high thicko. Try Again")
        If Answer < NumberToGuess Then MsgBox("Too Low chump. Try Again")
    End While
    MsgBox("Well done you guessed the right number")
End Sub

Solution

  • There are many problems in this question and your question is vague about which one you are talking about. So I'll list all of them here.

    1. You declare both NumberToGuess and Answer as Integer and assign it the result of an InputBox. But InputBox can return anything (number or alpha). And it will error out as soon as you try to assign the user's input to NumberToGuess. And that's before you check whether it is number or not.
    2. If you have OPTION STRICT ON it will show you compilation error "Option Strict On disallows implicit conversions from 'String' to 'Integer'". Keeping OPTION STRICT ON is a good practice in general and helps avoid innocent looking mistakes. e.g. Here you are assigning String type to Integervariable, which it doesn't allow.
    3. You have used a While loop with InputBox. There is no way for user to cancel out of the game unless they give the correct answer. The Cancel button of InputBox won't work.
    4. All your If conditions will be evaluated irrespective of previous one. I assume you want only one of the Message Boxes to be shown at a time. So you may want to make use of ElseIf too.

    To fix the problems, here we go:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NumberToGuess As String    '<-- declare as string because we will hold the result of InputBox in it.
        Dim Answer As String = ""      '<-- declare as string because we will hold the result of InputBox in it.
    
        NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
        While Answer <> NumberToGuess
            Answer = InputBox("Please enter your guess")
            If String.IsNullOrEmpty(Answer) Then Exit While '<-- if user pressed cancel button in InputBox.
            If Not IsNumeric(Answer) Then
                MsgBox("That ain't no number")
            ElseIf CInt(Answer) > CInt(NumberToGuess) Then
                MsgBox("Too high thicko. Try Again")
            ElseIf CInt(Answer) < CInt(NumberToGuess) Then
                MsgBox("Too Low chump. Try Again")
            Else
                ' neither less nor more. so this is the correct answer.
                MsgBox("Well done you guessed the right number")
                Exit While
            End If
        End While
    End Sub
    

    The above code however is a big annoyance because a MessageBox, then InputBox, thenMessageBox, thenInputBox... To fix this, you can show the message in the InputBox itself.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NumberToGuess As String    '<-- declare as string because we will hold the result of InputBox in it.
        Dim Answer As String = ""      '<-- replace with whatever answer you are expecting.
        Dim Message As String = "Please enter your guess"
    
        NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
        While Answer <> NumberToGuess
            Answer = InputBox(Message)
            If String.IsNullOrEmpty(Answer) Then Exit While '<-- if user pressed cancel button in InputBox.
            If Not IsNumeric(Answer) Then
                Message = "That ain't no number"
            ElseIf CInt(Answer) > CInt(NumberToGuess) Then
                Message = "Too high thicko. Try Again"
            ElseIf CInt(Answer) < CInt(NumberToGuess) Then
                Message = "Too Low chump. Try Again"
            Else
                ' neither less nor more. so this is the correct answer.
                MsgBox("Well done you guessed the right number")
                Exit While
            End If
        End While
    End Sub