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

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!




using sub procedure to calculate

 
Reply to this topicStart new topic

using sub procedure to calculate

liquid198
18 Nov, 2007 - 09:10 AM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 29


My Contributions
CODE

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

PsychoCoder
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 10:05 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

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

Hope this helps smile.gif
User is online!Profile CardPM
+Quote Post

liquid198
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 10:17 AM
Post #3

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 29


My Contributions
i made the corrections and im still getting a zero value.. and its not working.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 10:24 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Did you set the values for these 3 variables in your button click event?

CODE

Dim couponCode As Integer
Dim quantity As Integer
Dim total As Decimal


If you made the changes I suggested then post the new code, code.gif
User is online!Profile CardPM
+Quote Post

liquid198
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 10:39 AM
Post #5

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 29


My Contributions


CODE

Private Function CalculateTotalDue(ByVal couponCode As String, ByRef quantity As Integer) As Double



        Integer.TryParse(Me.xQuantityTextBox.Text, quantity)
        Dim totaldue As Double

        '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)
            Case Else
                Me.xTotalLabel.Text = "please enter the correct code"

        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 Integer
        Dim quantity As Integer
        Dim total As Decimal

        Integer.TryParse(Me.xQuantityTextBox.Text, quantity)
        Integer.TryParse(Me.xCodeTextBox.Text, couponCode)
        Integer.TryParse(Me.xTotalLabel.Text, total)

        Me.xTotalLabel.Text = Convert.ToDouble(CalculateTotalDue(couponCode, quantity



here is my new code posted.. without the to.upper.. it gave me errors
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 10:39 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

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

PsychoCoder
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 11:16 AM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

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

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


Hope this helps smile.gif
User is online!Profile CardPM
+Quote Post

liquid198
RE: Using Sub Procedure To Calculate
18 Nov, 2007 - 03:47 PM
Post #8

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 29


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

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:06PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month