Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Newbie programming *student* needs help...?

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 > 0 Then

total = Val(subtotalValueLabel.Text) + currentPrice

End If

End Sub

Update:

I forgot to add

Dim total As Decimal

Update 2:

I tried the first answer I was given and it still just shows the price I entered instead of a running subtotal. So if I enter 10.00 and click enter the subtotal is 10.00 but then if I want to add 15.00 and click enter- the subtotal says 15.00 instead of 25.00...

As for the second answer- I want the value of subtotal to be 0 initially and then add the price, so my total is what the first price I entered- but if I want to add a 2nd item's price into the gui- it should recognize that the total is the 1st items value and then subsequently add the next items price to the total (Does that make sense- it sounds like I'm talking in circles...) :( I keep checking back in my book to see what I am forgetting or messing up but in other assignments I have done this same thing and it worked. Could the exercise template I downloaded be messed up and causing me all this frustration? Ok- I know

I'm grasping for some sort of help or answers

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    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

    Source(s): software developer
  • 1 decade ago

    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

  • 1 decade ago

    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."

  • 4 years ago

    i ought to apply a for loop, and for simplicity count number the printing one after the different. int count number =2; //Initialize one counter (for the output) for(int i=a million;i<=10;i++){ //for loop, starts at one repeats 10 circumstances device.out.println(count number); //print the counter count number=count number+2; //increment counter } via ways i++ is a similar as putting forward i=i+a million

Still have questions? Get your answers by asking now.