Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-16
3
Medium Priority
?
1,523 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:wally_davis
3 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20089871
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
 
LVL 39

Accepted Solution

by:
appari earned 1600 total points
ID: 20090044
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
 

Assisted Solution

by:Shadi-Elwan
Shadi-Elwan earned 400 total points
ID: 20090057
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question