简体   繁体   中英

VB.Net Converting from string to double

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

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)

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

dim iVar as integer dim sStr as string

sstr=""

ivar = val(sstr)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM