【问题标题】:VB.Net Converting from string to doubleVB.Net 从字符串转换为双精度
【发布时间】:2011-05-11 21:40:55
【问题描述】:

我的程序中有一个严重的错误,如果用户在文本框中没有输入时按下检查(计算)按钮,程序会显示此错误:“从字符串转换”到类型“双”无效。”我想解决这个问题,但我不确定如何进行转换。我在想可能是 CType,但我听到的是解析。我该怎么做?文本框称为 mskTxtInput,按钮对象称为 btnCheck,它负责所有的计算和处理。

更新:这是我的代码,除了解析方法,希望对你有所帮助!

Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnCheck.Click pic1.Visible = False '隐藏图片 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

【问题讨论】:

  • 请分享一些出现此问题的代码。
  • 这与您的问题没有直接关系,但您似乎没有使用Option Strict。我建议你打开它:编译器会生成关于危险代码的警告,从长远来看这会节省你的时间。

标签: vb.net string textbox type-conversion


【解决方案1】:

尝试使用Double.TryParse Method (String, Double) 而不是

类似

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

returnValue = Double.TryParse(s, result)

【讨论】:

    【解决方案2】:

    使用TryParse方法进行解析,避免解析失败时出现异常:

    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
    

    【讨论】:

    • 嘿!我使用了您的代码,但对“解析通过”和“解析失败”进行了消息框修改,并不断收到“解析失败”,我可以清楚地显示代码,希望对您有所帮助!
    • @William Mc:您在发布的代码中在Then 之前放置了= 运算符,因此它不会运行。删除它,它的工作原理。我已经验证,如果解析成功,代码会显示第一个消息框。
    • 我去掉了 = 运算符,但它仍然不起作用:( VS 总是以黄色突出显示这一行: If mskTxtInput.Text = totalNum Then
    • 然而这表明解析工作: If mskTxtInput.Text = "" Then Dim value As Double Double.TryParse(mskTxtInput.Text, value) MsgBox("parsing working") ' 解析工作,所以使用这里的值 Else MsgBox("parsing failed") ' 解析失败,所以提醒用户这个事实 End If
    • @William Mc:您只是在检查输入是否为空。由于您不对 TryParse 方法的返回值做任何事情,因此您忽略了解析是否有效。即使解析失败,也会始终显示消息框。
    【解决方案3】:

    dim iVar 为整数 将 sStr 暗淡为字符串

    sstr=""

    ivar = val(sstr)

    【讨论】:

      【解决方案4】:

      使用静态方法Double.TryParse()。如果返回 true,则解析成功,您可以继续操作。如果它返回 false 则解析不成功,您应该显示一条错误消息(如果您愿意,请使用 MessageBox)并中止操作。

      【讨论】:

        猜你喜欢
        • 2012-06-23
        • 1970-01-01
        • 2012-03-14
        • 1970-01-01
        • 2011-08-11
        • 1970-01-01
        相关资源
        最近更新 更多