CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2015
    Posts
    8

    VB 2012 Can someone help me understanding coding in this example

    My assignment is as follows:

    • Collect and validate the number of items purchased and store the value in a variable
    • Use a Do….While loop to iterate a number of times equal to the number of items purchased
    • Collect the prices entered in an input box and store the values in an array
    • The loop in the previous step should contain an accumulator to determine subtotal
    • Create a module that applies a tax rate of 7% and calculate the total
    • Report the results on a second form name results.vb (see figure 2)
    • Assuming the user pays with a $100 bill, make change by using monetary denominations stored in an array. To loop through the values calculating change use a for next loop with a decreasing accumulator holding the value for change remaining
    • Display change on a third form named change.vb (see fig. 3)

    My subtotal is not calculating:

    Code:
     Public Cost As Decimal = 0
        Public Total As Decimal = 0
        Public TotalCost As Decimal = 0
    
    
        Private Sub btnCheckOut_Click(sender As Object, e As EventArgs) Handles btnCheckOut.Click
    
            Dim i As Integer = 0
    
            Dim Prices(CInt(txtItems.Text) - 1) As Decimal
    
            Do While i < CInt(txtItems.Text)
    
                Prices(i) = 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(Cost, decTAX_RATE)
                'Cost += Cost * decTAX_RATE
                'Return TotalCost
                'End Sub
    
                'End Module
    
    
                TotalCost = Calculate(Cost, decTAX_RATE)
    
                TotalCost += Prices(i)
                i += 1
            Loop
    
    
            For i = 0 To Prices.Count - 1
    
                frmResults.lstResults.Items.Add(Prices(i))
            Next
            ' Display the results in the in the Subtotal label on form Results.vb
    
            frmResults.lblSubtotal.Text = TotalCost.ToString("$ " & TotalCost)
    
            frmResults.ShowDialog()
    
            ' With 2 items input at one dollar each, this results in Subtotal of $11.00 ????
    
        End Sub


    Name:  Change Form.png
Views: 224
Size:  11.1 KBName:  Checkout Form.png
Views: 190
Size:  9.0 KBName:  Results Form.png
Views: 191
Size:  9.3 KB
    Last edited by Desparate_Noob; May 2nd, 2015 at 06:23 PM. Reason: Clarify question and post Form pics

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  3. #3
    Join Date
    Apr 2015
    Posts
    8

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

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  5. #5
    Join Date
    Apr 2015
    Posts
    8

    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.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  7. #7
    Join Date
    Apr 2015
    Posts
    8

    Re: VB 2012 Can someone help me understanding coding in this example

    Thanks.

  8. #8
    Join Date
    May 2015
    Posts
    1

    Re: VB 2012 Can someone help me understanding coding in this example

    Did you figure it out?!?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured