【问题标题】:I can't find the reason for InvalidCastException我找不到 InvalidCastException 的原因
【发布时间】:2022-11-24 15:52:45
【问题描述】:

我正在尝试为我的项目制作一个类似于在线商店的应用程序。代码一开始没问题(至少对我来说是这样,因为没有错误指示) 但是当我运行它并按下“添加到购物车”按钮时,它说 InvalidCastException 并说我正在尝试将字符串转换为双精度,即使我没有转换任何东西

这是我到目前为止所拥有的

Public Class Form1

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim prditem As ListViewItem
        Dim price, cartprice As Integer
        Dim product As String
        If clbParts.SelectedItem = 0 Then
            product = "Power Supply"
            price = 1
        End If
        If clbParts.SelectedItem = 1 Then
            product = "CPU"
            price = 2
        End If
       ....
        If rdo512gb.Checked = True Then
            product = "Hard Drive (512GB)"
            price = 11
        End If
        If rdo1tb.Checked = True Then
            product = "Hard Drice (1TB)"
            price = 12
        End If
        cartprice = Price * numQuantity.Text
        prditem = lvCart.Items.Add(product)
        With prditem
            .SubItems.Add(numQuantity.Text)
            .SubItems.Add(cartprice)
        End With
        If numQuantity.Text = "0" Then
            MessageBox.Show("Please put the number of items you want to add", "Cart Error")
        End If
        MessageBox.Show("Item/s is/are added to your cart", "Cart")
        numQuantity.Text = "0"
    End Sub

    Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
        Dim total As Integer = 0
        For i As Integer = 0 To lvCart.Items.Count - 1
            Dim quantity = CInt(lvCart.Items(i).SubItems(1).Text)
            Dim price = CInt(lvCart.Items(i).SubItems(2).Text)
            total += quantity + price
        Next
        txtTotal.Text = total.ToString("N2")
    End Sub

    Private Sub cboPayment_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPayment.SelectedIndexChanged
        If cboPayment.SelectedItem = 0 Then
            Label10.Enabled = True
            cboOnline.Enabled = True
        Else
            Label10.Enabled = False
            cboOnline.Enabled = False
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("Your items are being delivered in " + txtHomeAddress.Text + ", " + txtAddress.Text + "\n Please wait for the text confirmation", "Cart Error")
    End Sub
End Class

【问题讨论】:

标签: vb.net


【解决方案1】:

帮自己一个忙,观看有关如何在 Visual Studio 中进行调试的 YouTube 视频!

您需要做的是在此行上放置一个断点:

cartprice = Price * numQuantity.Text

将字符串转换为双精度

您正在将一个数字乘以一个字符串!你想做这样的事情:

cartprice = Price * Convert.ToDouble(numQuantity.Text)

我个人会做一个 double.TryParse 来查看它是否有效并在执行操作时使用输出值。

PS:作为挑战对自己,尝试将所有代码逻辑移动到业务逻辑类,然后使用 Bingings/BindingControls,以便将表示层与逻辑分离。这样你就可以对业务逻辑层进行单元测试了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多