Join 149,558 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,612 people online right now. Registration is fast and FREE... Join Now!
Private Sub CalculateTotalDue(ByVal couponCode As String, ByRef quantity As Integer) Integer.TryParse(Me.xQuantityTextBox.Text, quantity) 'calculates total due Select Case couponCode Case "H17" Me.xTotalLabel.Text = quantity + (2 * 0.5) Case "H75" Me.xTotalLabel.Text = quantity + (2 * 0.1) Case "H99" Me.xTotalLabel.Text = quantity + (2 * 0.25) End Select End Sub
Private Sub xExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCalcButton.Click Dim couponCode As Integer Dim quantity As Integer Dim total As Decimal Call CalculateTotalDue(couponcode, quantity) Me.xTotalLabel.Text = total.Tostring End Sub End Class
i have to use a sub procedure or function..i chose sub.. this app provides the user text boxes to enter coupon codes and quantity of item purchased.. everything in store is 2.00. YOu can see my case statement for coupon codes. i also need to make sure the letter int he coupon codes stays upper.. i wasnt sure where to put the TO.Upper.. and its not even calculating right. help thanks;
This post has been edited by PsychoCoder: 18 Nov, 2007 - 09:51 AM
Well, this is VB.Net not VB6 so stop using the Call Keyword, thats not in the .Net Library, its in the VB6 Library. Second, make CalculateTotialDue a Function not a sub. A sub doesnt return a value whereas a function does. Try something like this:
CODE
Private Function CalculateTotalDue(ByVal couponCode As String, ByRef quantity As Integer) As Double Integer.TryParse(Me.xQuantityTextBox.Text, quantity) 'Declare a variable to return the value in 'Dim totalDue As Double 'calculates total due Select Case couponCode Case "H17" totalDue = quantity + (2 * 0.5#) Case "H75" totalDue = quantity + (2 * 0.1#) Case "H99" totalDue = quantity + (2 * 0.25#) Case Else totalDue = quantity + (2 * 0.5#) End Select 'Return the total due variable to the calling method Return totalDue End Function
Private Sub xExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
'In this function I dont see where you're setting a 'value for couponCode,quantity or total Private Sub xCalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCalcButton.Click Dim couponCode As Integer Dim quantity As Integer Dim total As Decimal Me.xTotalLabel.Text = Convert.ToDouble(CalculateTotalDue(couponCode.ToUppser,quantity)) End Sub End Class
You will notice that in the CalculateTotalDue I use the # at the end of each of your double amounts, this will keep them in double amount otherwise the value will be treated as an integer and converted to 0 (zero). Also, in your button click event I dont see you setting the value of
couponCode
quantity
total
Without setting those values they will always compute as their default value which is 0 (zero). So you need to, somewhere in your button click even, set thos values before utilizing the CalculateTotalDue function.
Well I see one of your problems, in your button click event you have couponCode As Integer, but in your CalculateTotalDue function has coupon codes as H17, well H17 isn't an Integer value so I'm working why it isn't bombing on you whenever you try to calculate a total amount use.
How, I changed couponCode to a String data type, you had it as integer (and since it had a letter in it that just wont work). I looked at the last code you posted and you pretty much ifnored the ideas I offered to solve your problems, if someone is going to take the time to help the least you can do is try their examples
Second, in your button's click event you were using variables that were never assigned a value, meaning the string ones always had the value of String.empty and the integer ones always had the value of 0. The code I am about to post will work, I created a sample application and this code came directly from said application:
CODE
Private Function CalculateTotalDue(ByVal couponCode As String, ByRef quantity As Integer) As Double Integer.TryParse(Me.xQuantityTextBox.Text, quantity) Dim totaldue As Decimal 'calculates total due Select Case couponCode Case "H17" totaldue = quantity + (2 * 0.5) Case "H75" totaldue = quantity + (2 * 0.1) Case "H99" totaldue = quantity + (2 * 0.25) Case Else totaldue = quantity + (2 * 0.5) End Select Return totaldue End Function
Private Sub xExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xExitButton.Click Me.Close() End Sub
Private Sub xCalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCalcButton.Click Dim couponCode As String = Me.xCodeTextBox.Text Dim quantity As Integer = Integer.TryParse(Me.xQuantityTextBox.Text, New Integer) Dim total As Double = CalculateTotalDue(couponCode.ToUpper, quantity) Me.xTotalLabel.Text = String.Format("{0:c}", total) End Sub
yes it did.. i was not trying to be ugly by no means by not taking your suggestions.. i have been looking at this code it seems for hours and i did not see a few.. thank you for helping me finalize it and get it right..