Welcome to Dream.In.Code
Become a VB Expert!

Join 137,262 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,506 people online right now. Registration is fast and FREE... Join Now!




Subroutine Display

 
Reply to this topicStart new topic

Subroutine Display

the_dude
25 Nov, 2006 - 10:44 AM
Post #1

New D.I.C Head
*

Joined: 24 Nov, 2006
Posts: 5


My Contributions
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: 38
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Subroutine Display
25 Nov, 2006 - 12:52 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
QUOTE(the_dude @ 25 Nov, 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.

If you post a copy of the relevant code we may be able to assist you.
User is offlineProfile CardPM
+Quote Post

the_dude
RE: Subroutine Display
25 Nov, 2006 - 01:33 PM
Post #3

New D.I.C Head
*

Joined: 24 Nov, 2006
Posts: 5


My Contributions
QUOTE(KeyWiz @ 25 Nov, 2006 - 01:52 PM) *

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.

CODE
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


CODE
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


CODE
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


CODE
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

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Subroutine Display
25 Nov, 2006 - 04:05 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
CODE

      Dim decSubTotal As Decimal
      Dim decTax As Decimal

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

User is offlineProfile CardPM
+Quote Post

the_dude
RE: Subroutine Display
25 Nov, 2006 - 05:18 PM
Post #5

New D.I.C Head
*

Joined: 24 Nov, 2006
Posts: 5


My Contributions
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.

CODE
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




User is offlineProfile CardPM
+Quote Post

Jayman
RE: Subroutine Display
25 Nov, 2006 - 07:43 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

the_dude
RE: Subroutine Display
25 Nov, 2006 - 09:14 PM
Post #7

New D.I.C Head
*

Joined: 24 Nov, 2006
Posts: 5


My Contributions
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.


User is offlineProfile CardPM
+Quote Post

the_dude
RE: Subroutine Display
25 Nov, 2006 - 10:24 PM
Post #8

New D.I.C Head
*

Joined: 24 Nov, 2006
Posts: 5


My Contributions
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.

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


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

      'Disabled
      'Calculate()

      decProductPrice = priceArray(cboProducts.SelectedIndex)

      'subtotal
      decSubTotal = CDec(decProductPrice * UnitTotal)


CODE
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.gif My calculations are messed up, but I can't see where.

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


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:04PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month