错误是从字符串“”转换为类型'Double'无效

时间:2013-11-05 14:24:45

标签: vb.net

Private Sub btntambah_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btntambah.Click
    Dim n As Integer
    For n = 0 To lv1.Items.Count - 1
        If lv1.Items(n).Text = txtkdbrng.Text Then
            MsgBox("Data Buku Sudah Ada Dalam List")
            Exit Sub
        End If
    Next
    lv1.Items.Add(txtkdbrng.Text)
    lv1.Items(n).SubItems.Add(txtnmbrng.Text)
    lv1.Items(n).SubItems.Add(txtharga.Text)
    lv1.Items(n).SubItems.Add(txtjmlhhrg.Text)
    lv1.Items(n).SubItems.Add(txttotal.Text)
    lv1.Items(n).SubItems.Add(txtjmlpsn.Text)
    lv1.Items(n).SubItems.Add(txtspesifikasi.Text)
    txttotal.Text = Format(CDbl(Total()), "###, ###, ###")
    ClearBarang()
    txtkdbrng.Focus()
End Sub

Function Total() As Double
    Dim ttl As Double = 0
    If Not Double.TryParse(txttotal.Text, Total) Then
        Total = 0
    End If
End Function

Private Sub btncetak_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncetak.Click
    If txtnosph.Text = "" Or lv1.Items.Count = 0 Then
        MsgBox("Data Belum Lengkap", MsgBoxStyle.Information, "INFORMASI")
    Else
        Dim nilaikembali1, nilaikembali2 As Integer
        objsph.PNoSph = txtnosph.Text
        objsph.PTglSph = Format(dtpsph.Value, "yyy-MM-dd")
        objsph.PKdCs = txtkdcstmr.Text
        nilaikembali1 = objsph.Simpan()
        'menyimpan ke tabel Ada menggunakan perulangan
        For x As Integer = 0 To lv1.Items.Count - 1
            objada.PNoSph = txtnosph.Text
            objada.PKdBrg = lv1.Items(x).SubItems(1).Text
            objada.PKdBrg = CDbl(lv1.Items(x).SubItems(2).Text)
            objada.PKdBrg = CDbl(lv1.Items(x).SubItems(3).Text)
            nilaikembali2 = objada.Simpan()
            Dim objbarang As New ClsBarang
            objbarang.PKdBrg = lv1.Items(x).SubItems(0).Text
        Next
        If nilaikembali1 = 1 And nilaikembali2 = 1 Then
            MsgBox("Data Berhasil Disimpan", MsgBoxStyle.Information, "INFORMASI")
        End If
        ClearForm()
        ClearBarang()
        ClearCustomer()
        txtnosph.Text = objsph.AutoNumber
    End If
End Sub

2 个答案:

答案 0 :(得分:2)

问题在于此代码:

Function Total() As Double
    Dim ttl As Double = 0
    If Not Double.TryParse(txttotal.Text, Total) Then
        Total = 0
    End If
End Function

首先,您使用的是不太知名的VB6样式隐式返回变量,该变量与函数名称匹配,而是在名为Double的代码中使用声明的ttl并返回值,如下所示:

Function Total() As Double
    Dim ttl As Double = 0
    If Double.TryParse(txttotal.Text, ttl) Then
        Return ttl
    Else
        ' Attempted conversion of text to Double type failed
        ' Do something here, raise error, alert user, etc.
        ' Returning zero may or may not be acceptable, if it is return zero
        Return 0
    End If
End Function

使用Return语法可以产生更清晰的代码,因为它不会假设代码的读者知道与函数概念名称匹配的隐式变量。始终在Return

中使用Function

注意:如果总值为零并不表示存在问题,那么您需要更改函数以返回Nullable(Of Double)而不是Double,然后您可以更改{{1 } Else的部分结果为:

TryParse()

现在,Function Total() As Nullable(Of Double) Dim ttl As Double = 0 If Double.TryParse(txttotal.Text, ttl) Then Return ttl Else ' Attempted conversion of text to Double type failed ' So return null Return Nothing End If End Function 函数的来电者需要考虑返回Total并做出相应的反应。

答案 1 :(得分:1)

这意味着显式或隐式转换/转换为double的变量实际上是一个空字符串,无法继续。