错误`从字符串转换""输入' Double'在VB.net中无效

时间:2017-05-29 01:36:01

标签: vb.net

我正在尝试编写计算双值,但是它出现了一个错误,说Conversion from string "" to type 'Double' is not valid.

    Dim x As Integer = "8"
    Dim f As Integer = "100"
    Dim z As Double
    Dim r As Double
    Dim Nw As Double
    Dim W As Double
    Dim Was As Double
    Dim Nwas As Double
    Dim cost As Double
    Dim IBLP As Double
    Dim sale As Double
    Dim sale1 As Double
    Dim last As Double


    Nw = CDbl(txtnetwasage.Text)
    W = CDbl(txtweight.Text)
    Was = CDbl(txtwastage.Text)
    Nwas = CDbl(txtnetweight.Text)
    cost = CDbl(txtcost.Text)
    IBLP = CDbl(txtwastage.Text)
    sale = CDbl(txtsale.Text)
    sale1 = CDbl(txtvsale1.Text)
    last = Double.Parse(txtlast.Text)

    Nw = (W) * (Was)
    Nwas = (W) + (Nwas)
    cost = (IBLP) / (x) * (Nwas)
    z = (cost) / (f) * (sale)
    sale = (z) + (cost)
    r = (cost) / (f) * (sale1)
    last = (r) + (cost)

1 个答案:

答案 0 :(得分:1)

以下是Codexer在评论中建议的Double.TryParse()示例:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim W As Double
    If Not Double.TryParse(txtweight.Text, W) Then
        MessageBox.Show("Value: " & txtweight.Text, "Invalid Weight")
        txtweight.Focus()
        txtweight.SelectAll()
        Exit Sub
    End If

    Dim Nwas As Double
    If Not Double.TryParse(txtnetweight.Text, W) Then
        MessageBox.Show("Value: " & txtnetweight.Text, "Invalid Net Weight")
        txtnetweight.Focus()
        txtnetweight.SelectAll()
        Exit Sub
    End If

    ' ... etc ...

    ' ... everything is good, perform your calculations ...

End Sub