-
May 2nd, 2015, 02:20 PM
#1
VB 2012 Can someone help me understanding coding in this example
Last edited by Desparate_Noob; May 2nd, 2015 at 06:23 PM.
Reason: Clarify question and post Form pics
-
May 3rd, 2015, 09:33 AM
#2
Re: VB 2012 Can someone help me understanding coding in this example
A few things
1st off you are setting totalcost to the value returned from your function so it will never add the prices together it will instead hold the last price on the list.
Cost is defined as public in your module and local in your form so cost in the form code is a different variable than the one in the module.
Cost has not be assigned a value so the funtion will always return 0.
What you should be doing is passing price to the function that calculates the tax on the item then adding the return value to the total cost
So these two lines
Code:
TotalCost = Calculate(Cost, decTAX_RATE)
TotalCost += Prices(i)
become
Code:
TotalCost += Calculate(Prices(i), decTAX_RATE)
That said you really should not be calling the function in the loop but after the loop so that rather than calculate tax for each item it would caculate tax on the total so it would be.
Code:
SubTotal += Prices(i)
i += 1
Loop
TotalCost = Calculate(subTotal, decTAX_RATE)
The way you are doing it does not provide a sub total as a sub total is the total before taxes
Always use [code][/code] tags when posting code.
-
May 3rd, 2015, 12:16 PM
#3
Re: VB 2012 Can someone help me understanding coding in this example
Okay, so I made the following changes:
Code:
' Loop counter
Dim intCount As Integer = 0
' Declare a variable to hold the Calculate function
Dim Calculate As Decimal = 0
'Declare a variable to hold the TotalCost
Dim TotalCost As Decimal = 0
Dim Prices(CInt(txtItems.Text) - 1) As Decimal
Do While intCount < CInt(txtItems.Text)
Prices(intCount) = CDec(InputBox("Put the value of your purchase", "Purchase Value", "0", 250, 250))
'Call the Calculate function from the TaxCalc module (Included as comment for reference)
'Module TaxCalc
' Declare constant for tax rate
'Public Const decTAX_RATE As Decimal = 0.07D
' Declare a variable to hold the total cost
'Dim TotalCost As Decimal = 0
'Public Function Calculate(Subtotal, decTAX_RATE)
'TotalCost = Subtotal + (Subtotal * decTAX_RATE)
'Return TotalCost
'End Sub
'End Module
frmResults.lblSubtotal.Text += Prices(intCount)
intCount += 1
Loop
'Call the Calculate function
TotalCost = Calculate(Subtotal, decTAX_RATE)
Now I get the following error:
Code:
'Call the Calculate function
TotalCost = Calculate(Subtotal, decTAX_RATE)
**{Calculate} Expression is not an array or a method, and cannot have an argument list.**
This project is SOOO frustrating. I haven't even gotten to the form to display the change denominations . . . .
-
May 3rd, 2015, 02:24 PM
#4
Re: VB 2012 Can someone help me understanding coding in this example
show the actual code rather than sticking it in as a comment.
Your function is not defined properly you need to give it a return type
Code:
Public Function Calculate(Subtotal, decTAX_RATE) As Decimal
The error kind of sounds like you have the code in the module commented out
Always use [code][/code] tags when posting code.
-
May 3rd, 2015, 03:45 PM
#5
Re: VB 2012 Can someone help me understanding coding in this example
Code:
Module TaxCalc
' Declare constant for tax rate
Public Const decTAX_RATE As Decimal = 0.07D
' Declare a variable to hold the total cost
Dim TotalCost As Decimal = 0
Public Function decCalculate(Subtotal, decTAX_RATE) As Decimal
TotalCost = Subtotal + (Subtotal * decTAX_RATE)
Return TotalCost
End Function
End Module
Code:
Public Subtotal As Decimal = 0
Private Sub btnCheckOut_Click(sender As Object, e As EventArgs) Handles btnCheckOut.Click
' Loop counter
Dim intCount As Integer = 0
Dim Prices(CInt(txtItems.Text) - 1) As Decimal
Do While intCount < CInt(txtItems.Text)
Prices(intCount) = CDec(InputBox("Enter the value of your purchase", "Purchase Value", "0", 250, 250))
frmResults.lblSubtotal.Text += Prices(intCount)
intCount += 1
Loop
' Declare a variable to hold the Calculate function
Dim Calculate As Decimal = 0
'Declare a variable to hold the TotalCost
Dim TotalCost As Decimal = 0
'Call the Calculate function
TotalCost = decCalculate(Subtotal, decTAX_RATE)
For intCount = 0 To Prices.Count - 1
frmResults.lstResults.Items.Add(Prices(intCount))
Next
' Display the results in the in the Subtotal label on form Results.vb
frmResults.lblSubtotal.Text = TotalCost.ToString("$" & TotalCost)
frmResults.ShowDialog()
End Sub
Now I'm getting an error message for this line of code:
Code:
frmResults.lblSubtotal.Text += Prices(intCount)
"Conversion from string "" to type 'Double' is not valid."
It was not giving this error message earlier.
-
May 3rd, 2015, 08:02 PM
#6
Re: VB 2012 Can someone help me understanding coding in this example
Don't try to add directly to the text property. Instead you should use a variable that is of type decimal and add to that then when you are done with the loop you place the value in your textbox.
Textboxes are meant for display and entry. They are not meant to be used for math operations
Always use [code][/code] tags when posting code.
-
May 4th, 2015, 01:43 PM
#7
Re: VB 2012 Can someone help me understanding coding in this example
-
May 5th, 2015, 01:02 PM
#8
Re: VB 2012 Can someone help me understanding coding in this example
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|