7 Replies - 3368 Views - Last Post: 25 November 2006 - 11:24 PM Rate Topic: -----

#1 the_dude  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 24-November 06

Subroutine Display

Posted 25 November 2006 - 11:44 AM

Hey,

I'm having a problem getting subtotal and tax displayed from a subroutine into a listbox in VB.NET. I was trying to get the subtotal, tax, and total displayed once a calculate button was pressed. The total is a class level variable and displays with the right calculation I think, but the subtotal and tax give just zeroes. I'm not sure if I'm calling the sub correctly to the calculate button procedure.

Can anyone point me in the right direction with this? Thanks.

Attached File(s)

  • Attached File  CCT.zip (838.43K)
    Number of downloads: 94


Is This A Good Question/Topic? 0
  • +

Replies To: Subroutine Display

#2 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Subroutine Display

Posted 25 November 2006 - 01:52 PM

View Postthe_dude, on 25 Nov, 2006 - 11:44 AM, said:

Hey,

I'm having a problem getting subtotal and tax displayed from a subroutine into a listbox in VB.NET. I was trying to get the subtotal, tax, and total displayed once a calculate button was pressed. The total is a class level variable and displays with the right calculation I think, but the subtotal and tax give just zeroes. I'm not sure if I'm calling the sub correctly to the calculate button procedure.

Can anyone point me in the right direction with this? Thanks.

If you post a copy of the relevant code we may be able to assist you.
Was This Post Helpful? 0
  • +
  • -

#3 the_dude  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 24-November 06

Re: Subroutine Display

Posted 25 November 2006 - 02:33 PM

View PostKeyWiz, on 25 Nov, 2006 - 01:52 PM, said:

If you post a copy of the relevant code we may be able to assist you.


Ok, thanks. With the code below, the total is calculating fine, but the subtotal and tax display inaccurate amounts.

Private Sub btnCalculateTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateTotal.Click
	  Dim decSubTotal As Decimal
	  Dim decTax As Decimal

	  'results in inaccurate total if enabled
	  'Calculate(decSubTotal, decTax)

	  'call to Sub DisplayOutput
	  DisplayOutput(decSubTotal, decTax, TotalPrice)
   End Sub


Private Sub btnAddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddProduct.Click
	  'declare variables
	  Dim decSubTotal As Decimal
	  Dim strName As String		 'selected product
	  Dim UnitTotal As Single	   'number of products
	  Dim decTax As Decimal

	  'UnitTotal assignment
	  UnitTotal += CSng(Val(txtUnits.Text))

	  'call to Sub Calculate
	  Calculate(decSubTotal, decTax)

	  'Display product selection in listbox
	  strName = CStr(cboProducts.SelectedItem)
	  If strName = "" Then
		 MessageBox.Show("Please select an item", "No selection detected", _
		 MessageBoxButtons.OK, MessageBoxIcon.Error)
	  Else
		 Dim frmtStr As String = "{0, 44}" & ControlChars.Tab & "{1:N}"
		 lstDisplay.Items.Add(strName)
		 lstDisplay.Items.Add(String.Format(frmtStr, "Units:", UnitTotal))
	  End If
   End Sub


Private Sub DisplayOutput(ByVal decSubTotal As Decimal, ByVal decTax As Decimal, _
   ByVal TotalPrice As Decimal)

	  Calculate(decSubTotal, decTax)


	  Dim frmtStr As String = "{0, 44}" & ControlChars.Tab & "{1:N}"
	  lstDisplay.Items.Add( _
   "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
	  lstDisplay.Items.Add(String.Format(frmtStr, "Subtotal:", FormatCurrency(decSubTotal)))
	  lstDisplay.Items.Add(String.Format(frmtStr, "Tax:", FormatCurrency(decTax)))
	  lstDisplay.Items.Add(String.Format(frmtStr, "Total:", FormatCurrency(TotalPrice)))
   End Sub


Sub Calculate(ByRef decSubTotal As Decimal, ByRef decTax As Decimal)
	  Dim decProductPrice As Decimal

	  'declare constants
	  Const TAX As Single = 0.089

	  'UnitTotal assignment
	  UnitTotal += CSng(Val(txtUnits.Text))

	  'price of product assignment, selected
	  decProductPrice = (priceArray(cboProducts.SelectedIndex))

	  'subtotal
	  decSubTotal = CDec(decProductPrice * UnitTotal)

	  'tax
	  decTax = CDec(decSubTotal * TAX)

	  'total price
	  TotalPrice = (decSubTotal + decTax)
   End Sub

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Subroutine Display

Posted 25 November 2006 - 05:05 PM

You are calling correctly to the Calculate method but you are not giving the method any values in the variable that are being passed to the method. Since you never assign a value to decSubTotal and decTax, the default value of 0 is assigned to them for you. Now you are basically sending this information to your method Calculate(0, 0) and what happens when you use 0 as a multiplier in your equation, you get 0 back.
	  Dim decSubTotal As Decimal
	  Dim decTax As Decimal

	  'results in inaccurate total if enabled
	  'Calculate(decSubTotal, decTax)


Was This Post Helpful? 0
  • +
  • -

#5 the_dude  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 24-November 06

Re: Subroutine Display

Posted 25 November 2006 - 06:18 PM

So, I assign decSubTotal and decTax in this procedure like below? This calculates two of the selected products if I input three, and the TotalPrice looks like it only calculates one product entered. I tried just calling the procedure like this Calculate(), but that didn't work either. Sorry, I'm new at this.

Private Sub btnCalculateTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateTotal.Click
	  Dim decSubTotal As Decimal
	  Dim decTax As Decimal
	  Dim decProductPrice As Decimal

	  'declare constants
	  Const TAX As Single = 0.089

	  'price of product assignment, selected
	  decProductPrice = (priceArray(cboProducts.SelectedIndex))

	  'subtotal
	  decSubTotal = CDec(decProductPrice * UnitTotal)

	  'tax
	  decTax = CDec(decSubTotal * TAX)

	  'Call to Sub Calculate
	  Calculate(decSubTotal, decTax)

	  'Call to Sub DisplayOutput
	  DisplayOutput(decSubTotal, decTax, TotalPrice)
   End Sub

Was This Post Helpful? 1
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Subroutine Display

Posted 25 November 2006 - 08:43 PM

You are going to need a class level variable so you can store a running subtotal.

Then every time an item is added, its extended price will have to be added to the subtotal variable.

The tax does not need to be class level, as it can be determined from the subtotal value.
Was This Post Helpful? 1
  • +
  • -

#7 the_dude  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 24-November 06

Re: Subroutine Display

Posted 25 November 2006 - 10:14 PM

Ok, I've added the SubTotal. I also added it to the btnAddProduct event procedure with an assignment. It's still calculating one more product than it should if I move all of the calculations to the btnCalculateTotal and disable the Sub Calculate procedure. If I keep them in the Sub Calculate procedure, it calculates one no matter how many are entered.

I'm calling the Sub Calculate procedure to both btn procedures when the calculations aren't in the btnCalculateTotal procedure. Is this part of the problem?

I was able to get the UnitTotal to count the units. It displayed 1, 2 and so on next to each product in the listbox, while the SubTotal calculated one more than it should have.
Was This Post Helpful? 1
  • +
  • -

#8 the_dude  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 24-November 06

Re: Subroutine Display

Posted 25 November 2006 - 11:24 PM

After playing with this for a bit, it calculates the correct amount if I add each item one at a time, but incorrectly with inputs of multiples of items.

 Private Sub btnCalculateTotal_Click
 'Call to Sub DisplayOutput
	  Calculate()
	  DisplayOutput(decSubTotal, decTax, TotalPrice)


Private Sub btnAddProduct_Click
 'UnitTotal assignment
	  UnitTotal += CSng(Val(txtUnits.Text))

	  'Disabled
	  'Calculate()

	  decProductPrice = priceArray(cboProducts.SelectedIndex)

	  'subtotal
	  decSubTotal = CDec(decProductPrice * UnitTotal)


Sub Calculate()
'subtotal
	  decSubTotal = (decSubTotal - decProductPrice) + CDec(decProductPrice * UnitTotal)


With the only change in the code below, I get a calculation for only one item if I add each item one at a time. And it correctly calculates multiple items at once. :crazy: My calculations are messed up, but I can't see where.

Sub Calculate()
'subtotal
	  decSubTotal = CDec(decProductPrice * UnitTotal)

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1