-2

Confusing part for me I am trying to make an app like an online shop for my project. The code is fine at first(at least for me since there's no errors indications) But when I run it and press the "Add to Cart" button, it says InvalidCastException and says that I'm trying to convert a string to double even though I am not converting anything

This is what I have so far

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
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 4
    As a first step, you should enable [`Option Strict`](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement) so that the compiler prevents you from doing those kinds of mistakes. See [What do Option Strict and Option Explicit do?](https://stackoverflow.com/q/2454552/8967612) and [Can I set Option Explicit and Option Strict on a Project/Solution level?](https://stackoverflow.com/q/5076851/8967612) – 41686d6564 stands w. Palestine Nov 24 '22 at 07:58

1 Answers1

1

Do yourself a favor and watch a YouTube video on how to debug in Visual Studio!

What you need to do is put a Breakpoint on this line:

cartprice = Price * numQuantity.Text

convert a string to double

You're multplying a number by a String! You want to do something like this:

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

I'd personally do a double.TryParse to see if its valid and use the output value when performing the operation.

PS: As a challenge to yourself, try and move all the code logic to a Business Logic class, then use Bingings/BindingControls so the Presentation layer is separated from the logic. That way you can Unit Test the business logic layer!

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • The operation is not the problem. It says that the string "Power Supply" cannot be converted to Double, but I'm not converting that part tho – Leonard Sinining Nov 24 '22 at 08:38
  • So you're saying that numQuantity.Text contains the value "Power Supply" and you're multiplying that by a numeric price value? – Jeremy Thompson Nov 24 '22 at 08:39
  • No, I'm saying that the problem that it says that I'm converting the string "Power Supply" into a Double, but I'm not converting it – Leonard Sinining Nov 24 '22 at 08:41
  • I'm not able to see that mate, I can only go off what the debugger is telling me, and I believe it when it says you're converting Power Supply to a Double. Did you watch a Debug Video? **Edit** your question showing me a screenshot of the code control halted on a breakpoint with txtQuantity.text showing a numeric value? – Jeremy Thompson Nov 24 '22 at 08:44
  • There, I put the image link(since im not allowed to put pics yet) – Leonard Sinining Nov 24 '22 at 09:45
  • @LeonardSinining The screenshot provided zero additional information. Put a breakpoint on the exact line that throws the exception and inspect the values being calculated. If you still can't figure it out, post the full [exception details](https://idownvotedbecau.se/noexceptiondetails/) (including the stack trace). Or, like I said above, save yourself so much trouble and enable `Option Strict`. – 41686d6564 stands w. Palestine Nov 24 '22 at 13:05
  • @LeonardSinining you haven't followed any of my advice. **Do yourself a favor and watch a YouTube video on how to debug in Visual Studio!** – Jeremy Thompson Nov 24 '22 at 22:51