VB.NET InputBox "InvalidCastException was unhandled" error when clicking on the Cancel button

In VB.NET, when I click on the InputBoxes "Cancel" button, I get an "InvalidCastException was unhandled" error. essentially, this program is just a test whereby the User picks a number, a random number is generated, the two are compared and an Image moves around on the Form depending on whether your guess is higher or lower than the computers (randomGenerator) randomly generated number.
Here is the VB.NET Code: (The user clicks on a Button named "Generate Number", which then presents an InputBox where the User must enter a number from 10 to 100).

Private Sub generateNumberButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles generateNumberButton.Click
        Dim userChoice As String
        Static numberCorrect As Integer
        Static numberIncorrect As Integer

        ' Save the user's Listbox selection
        ' userChoice = pickNumberListBox.SelectedItem.ToString
        userChoice = InputBox("Please pick a number from 10 to 100", "Pick A Number", "10")

        randomNumber = randomGenerator.Next(10, 101)
        cgnDisplayLabel.Text = Convert.ToString(randomNumber)


        If CDbl(userChoice) < randomNumber Then  ' <-- The CastException happens here and possibly in th
                                                                               ' next ElseIf condition
            tryAgainPictureBox.SetBounds(479, 41, 0, 0, BoundsSpecified.Location)
        ElseIf CDbl(userChoice) > randomNumber Then
            tryAgainPictureBox.SetBounds(479, 103, 0, 0, BoundsSpecified.Location)
        Else
            MessageBox.Show("The number you selected matches the computers number", _
            "The numbers match", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

Any ideas on how to re-write the code  to prevent the InvalidCastException?

Thank you,
Wally
wally_davisAsked:
Who is Participating?
 
appariCommented:
try changing the condition as follows
if userChoice is nothing orelse userChoice="" then
 MessageBox.Show("you haven't selected The number ", _
            "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)

elseIf CDbl(userChoice) < randomNumber Then  ' <-- The CastException happens here and possibly in th
                                                                               ' next ElseIf condition
            tryAgainPictureBox.SetBounds(479, 41, 0, 0, BoundsSpecified.Location)
        ElseIf CDbl(userChoice) > randomNumber Then
            tryAgainPictureBox.SetBounds(479, 103, 0, 0, BoundsSpecified.Location)
        Else
            MessageBox.Show("The number you selected matches the computers number", _
            "The numbers match", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
0
 
JimBrandleyCommented:
Set a breakpoint on this line:
     If CDbl(userChoice) < randomNumber Then  

When you hit it, Quickwatch userChoice to see what you got back.

Jim
0
 
Shadi-ElwanCommented:
That is because when you click ""Cancel"" button the (((userChoice))) value is NOTHING (null in C#)

userChoice = InputBox("Please pick a number from 10 to 100", "Pick A Number", "10")

SO when you want to covert (((userChoice))) to a double type : CDbl(userChoice) it is impossible to convert nothing to double value cause there no value in (((userChoice))) in the case of clicking ""Cancel"".

Hope that you accept it as an answer :)
Have a nice day ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.