1

I have a bad bug in my program where if a user presses the check(calculate) button when there is no input in the textbox the program displays this error: "Conversion from string "" to type 'Double' is not valid." I would like to resolve this but I am not sure how to do the conversion. I was thinking possibly CType but I am hearing talk of parsing. How do I go about this? the textbox is called mskTxtInput and the button object is called btnCheck which does all the calculation and processing.

Update: This is my code except the parsing method so hope this helps a little!

Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click pic1.Visible = False 'hide picture pic1.Image = My.Resources.A pic2.Image = My.Resources.F

    Dim value As Double
    If Double.TryParse(mskTxtInput.Text, value) = Then
        MsgBox("parsing success") ' parsing worked, so use the value in here 
    Else
        MsgBox("parsing failed") ' parsing failed, so alert the user to that fact 
    End If


    If radAdd.Checked = True Then
        totalNum = num1 + num2


    End If

    If radSub.Checked = True Then
        totalNum = num1 - num2

    End If

    If radMulti.Checked = True Then
        totalNum = num1 * num2



    End If

    If mskTxtInput.Text = totalNum Then
        lblAns.Text = ("Correct!")
        lblAns2.Text = ("Answer is " & totalNum)
        pic1.Visible = True
        wins = wins + 1
        nScore = wins



    Else
        lblAns.Text = ("Incorrect")
        lblAns2.Text = ("Answer should be " & totalNum)
        pic2.Visible = True

    End If

    attempts = attempts + 1
    If attempts = 5 Then
        MessageBox.Show("Game Finished! ", "End Of Game", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Exclamation)
        lblAns.Text = ("You scored " & wins & " Out of 5")
        btnSpin.Enabled = False
        pic1.Visible = False
        pic2.Visible = False
        lblAns2.Text = ""
        lblAns2.Text = "Play again?"
        btnCheck.Enabled = False
        btnNew.Enabled = True
        attempts = 0
        wins = 0
    End If


    mskTxtInput.Clear()
    mskTxtInput.Focus()


End Sub
2
  • Please share some code where this problem happens. Commented Nov 18, 2010 at 8:30
  • This isn't directly relevant to your question, but it looks like you aren't using Option Strict. I recommend you turn that on: the compiler then generates warnings about dangerous code that will save you time in the long run. Commented Nov 18, 2010 at 11:17

4 Answers 4

4

Use the TryParse method to do the parsing to avoid getting an exception if the parsing fails:

Dim value As Double
If Double.TryParse(mskTxtInput.Text, value) Then
  ' parsing worked, so use the value in here
Else
  ' parsing failed, so alert the user to that fact
End If
Sign up to request clarification or add additional context in comments.

7 Comments

Hey! I used your code but made messagebox admendments of "parsing passed" and "parsing failed" and kept receiving the "parsing failed" I can show the code obviously and hope it helps a little!
@William Mc: You have put an = operator before Then in the code that you posted, so it won't run. Remove that and it works. I have verified that the code shows the first message box if the parsing succeeds.
I took away the = operator but it still did not work :( VS always highlights this line in yellow: If mskTxtInput.Text = totalNum Then
This however stated the parsing worked: If mskTxtInput.Text = "" Then Dim value As Double Double.TryParse(mskTxtInput.Text, value) MsgBox("parsing worked") ' parsing worked, so use the value in here Else MsgBox("parsing failed") ' parsing failed, so alert the user to that fact End If
@William Mc: You are just checking if the input is empty. As you don't do anything with the return value from the TryParse method, you are ignoring whether the parsing worked or not. The messagte box is always shown even if the parsing fails.
|
4

Try using Double.TryParse Method (String, Double) rather

Something like

Dim s As String
Dim result As Double
Dim returnValue As Boolean

returnValue = Double.TryParse(s, result)

Comments

0

dim iVar as integer dim sStr as string

sstr=""

ivar = val(sstr)

Comments

0

Use the static method Double.TryParse(). If it returns true then parsing was successful, and you can proceed with the operation. If it returns false then parsing was not successful, and you should show an error message (using MessageBox if you desire) and abort the operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.