11 Mar

What am I doing wrong in Visual Basic?

Posted by cfz under enart.hubeidaxue.com

  • I am trying to do create a program that calculates sales calculations. I am using If statements to build my program as that quantity purchased is based on sales discount rate.

    Everytime I go to debug my program, I get something along the lines of $0.06 in my sales price per item box, when it should be much higher. and the rest of my text boxes are displaying the infamous $0.00. I am at a total loss on what the issue is, I know that VB was having an issue with one of my domains but it "fixed" it.

    I have 7 text boxes that say: Quantity Purchased, Original Price Per Item(these are imputted by the user), Sales Discount Rate, Sales Price Per Item, Discounted Sales Total, Sales Tax Due and total amount due.

    Here is my code. I really appreciate anyone taking the time to help me on this. Im at a total loss on what could be the issue.

    Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
    Dim sngquantity As Single, sngoriginalprice As Single, sngsalesprice As Single, sngdiscountsales As Single, sngsalestax As Single

    sngquantity = CSng((Val(txtquantity.Text)))
    sngoriginalprice = CSng(Val(txtoriginalprice.Text))
    sngsalesprice = CSng(Val(txtsalesprice.Text))
    sngdiscountsales = CSng(Val(txtdiscounttotal.Text))
    sngsalestax = CSng(Val(txtsalestax.Text))



    If sngquantity <= 10 Then
    txtsalesdiscount.Text = "15%"
    txtsalesprice.Text = FormatCurrency(CStr((sngoriginalprice - (sngoriginalprice * 0.05)) / 100)).ToString

    ElseIf sngquantity <= 20 Then
    txtsalesdiscount.Text = "10%"
    txtsalesprice.Text = FormatCurrency(CStr((sngoriginalprice - (sngoriginalprice * 0.1)) / 100)).ToString

    ElseIf sngquantity >= 21 Then
    txtsalesdiscount.Text = "15%"
    txtsalesprice.Text = FormatCurrency(CStr((sngoriginalprice - (sngoriginalprice * 0.15)) / 100)).ToString

    End If

    txtdiscounttotal.Text = FormatCurrency(sngquantity * sngsalesprice)
    txtsalestax.Text = FormatCurrency(sngdiscountsales * 0.07).ToString
    txttotalamount.Text = FormatCurrency(sngdiscountsales + sngsalestax)

    End Sub



  • Its a bit of pain to go through the code and pinpoint an error, you'd probably benefit more from learning general debugging techniques which you can use in future.

    Most programmers don't just stare at the code until they see the problem, they use the process of elimination. Your code isn't too long, but if its much longer then you can usually make an educated guess at roughly the area that the problem is located by wha tis happening.

    Anyway, you need to display the value of your variables throughout each step of the calculation,(alongside displaying something unique to each line, so you can tell which display relates to which part of the code) you can then pinpoint at what point things go wrong, and how, from there it should be a lot easier to work out what the problem is







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about What am I doing wrong in Visual Basic? , Please add it free.
    | |