I have an assignment that we have an existing GUI and all I have to do is code is in VB.Net to function like a cash register. seems easy enough right? but remember I'm just learning...so I have a textbox the user inputs a price, a label to have a running subtotal, a label for the tax amount, and a label for the grand total. Also the gui has an enter button which is clicked after each price and will add to the subtotal, then there is a total button that when clicked figures the tax amount and displays it as well as the grand total is figures and displayed.
so here is what I have for the enter button (just to add the prices for a subtotal) but every time I click a new price- and then enter the new price isn't added to the subtotal- instead the subtotal is whatever price I just typed in... so here is my code. Can someone tell me what I am missing or have done wrong?
Dim currentPrice As Decimal
Dim subtotal As Decimal
currentPrice = Val(currentPriceTextBox.Text)
'add the items values
total = subtotal + currentPrice
'display subtotal
subtotalValueLabel.Text = String.Format("{0:C}", total)
'clear item price box
currentPriceTextBox.Text = ""
If currentPrice %26gt; 0 Then
total = Val(subtotalValueLabel.Text) + currentPrice
End If
End Sub
Newbie programming *student* needs help...?
The advise to do subtotal = subtotalValueLabel.text whould be correct one but not in your case. Because you format the lable text to show a dollar sign before the price, it would requeir more code to convert it back to a number. That is why subtotal always becomes zero.
The easiest way in your case is to declare subtotal as a global variable. Meaning it should be decared outside of your Sub procedure. The code will look something like this:
Dim subtotal As Decimal
'this is the procedure for click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim currentPrice As Decimal
currentPrice = Val(currentPriceTextBox.Text)
'add the items values
subtotal = subtotal + currentPrice
'display subtotal
subtotalValueLabel.Text = String.Format("{0:C}", subtotal)
'clear item price box
currentPriceTextBox.Text = ""
End Sub
you would have to do similar thing to calculate grand total
Reply:Its because you have not initialised the variable subtotal
You have declared subtotal
Dim subtotal As Decimal
And then straight away used it in the equation
total = subtotal + currentPrice
At this stage, since subtotal does not have any value and hence is euqial to zero.
hence total = 0 + currentPrice which is equal to currentPrice
Reply:The problem is that when you declare "Dim subtotal As Decimal", you are essentially setting "subtotal" to zero. You need to save the existing value of subtotalValueLabel as subtotal before doing the "total = subtotal + currentPrice." Add something like "subtotal = subtotalValueLabel.text" after "Dim subtotal As Decimal."
survey questions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment