Results 1 to 6 of 6

Thread: hw problem functions and sub procedures

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    5

    hw problem functions and sub procedures

    Code:
    Public Class Frmbilling
    
        Dim itemamount, tax, subtotal, total As Double
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
            getitem()
            calctotal()
            calctax()
            calcsubtotal()
            displaytotal()
    
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    
            Rdbcappuccino.Checked = False
            RdbEspresso.Checked = False
            rdbLatte.Checked = False
            RdbIcedLatte.Checked = False
            RdbIcedCappuccino.Checked = False
            TxtQuantity.Clear()
            TxtItemAmount.Clear()
            chkTax.Checked = False
            TxtQuantity.Focus()
    
        End Sub
    
        Private Sub btnNewOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOrder.Click
            Rdbcappuccino.Checked = False
            RdbEspresso.Checked = False
            rdbLatte.Checked = False
            RdbIcedLatte.Checked = False
            RdbIcedCappuccino.Checked = False
    
            TxtQuantity.Clear()
            TxtItemAmount.Clear()
    
            chkTax.Checked = False
            TxtQuantity.Focus()
            Txtsubtotal.Clear()
            TxtItemAmount.Clear()
            txtTax.Clear()
            TxtTotal.Clear()
    
            'CalcGrandTotal()
            'CalcAddOneCustomer()
    
    
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    
            End
    
        End Sub
    
        Sub getitem()
    
            Dim quantity As Double
            TxtQuantity.Text = quantity
            If Rdbcappuccino.Checked Then
                TxtItemAmount.Text = 2 * quantity
            ElseIf RdbEspresso.Checked Then
                TxtItemAmount.Text = 2.25 * quantity
            ElseIf rdbLatte.Checked Then
                TxtItemAmount.Text = 1.75 * quantity
            ElseIf RdbIcedLatte.Checked Then
                TxtItemAmount.Text = 2.5 * quantity
            ElseIf RdbIcedCappuccino.Checked Then
                TxtItemAmount.Text = 2.5 * quantity
    
            End If
        End Sub
    
        Private Function calcsubtotal()
    
            Dim sub1 As Double
            subtotal = sub1 + itemamount
            Return (subtotal)
    
        End Function
    
        Private Function calctax()
    
            Dim subtotal As Double
            If chkTax.Checked Then
                tax = subtotal * 0.08
            End If
            Return (tax)
    
        End Function
    
        Private Function calctotal()
    
            total = subtotal + tax
            Return (total)
    
        End Function
        Sub displaytotal()
    
            Txtsubtotal.Text = subtotal
            txtTax.Text = tax
            TxtTotal.Text = total
    
        End Sub
    End Class

    i dont know exactly where to go from here, i am getting 0 in all my textboxes, i dont really understand functions and subprocedures and byval and by ref, so can someone help me to figure out what i am doing wrong.
    this is a site with the same project on it heres the link if it helps you to understand what i need to accomplish
    http://www.shsu.edu/~mis_gab/MIS%202...Chapter8HW.pdf

    thanks-

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: hw problem functions and sub procedures

    vb Code:
    1. TxtQuantity.Text = quantity
    ???

    shouldn't that be:

    vb Code:
    1. quantity = TxtQuantity.Text

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: hw problem functions and sub procedures

    first of all, those functions should have a return datatype. i.e.

    vb Code:
    1. Private Function calctotal() as double

    the function calls in btnCalculate_Click are incorrectly written and need to be in the right order

    vb Code:
    1. itemamount = getitem()
    2. tax = calctax()
    3. subtotal = calcsubtotal()
    4. total = calctotal()
    5. displaytotal()

    getItem should be a function with a return value

    vb Code:
    1. Function getitem()as string
    2.  
    3.      Dim quantity As Double
    4.      quantity = TxtQuantity.Text
    5.      If Rdbcappuccino.Checked Then
    6.         TxtItemAmount.Text = 2 * quantity
    7.      ElseIf RdbEspresso.Checked Then
    8.         TxtItemAmount.Text = 2.25 * quantity
    9.      ElseIf rdbLatte.Checked Then
    10.         TxtItemAmount.Text = 1.75 * quantity
    11.      ElseIf RdbIcedLatte.Checked Then
    12.         TxtItemAmount.Text = 2.5 * quantity
    13.      ElseIf RdbIcedCappuccino.Checked Then
    14.         TxtItemAmount.Text = 2.5 * quantity
    15.      End If
    16.      return TxtItemAmount.Text
    17. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    5

    Re: hw problem functions and sub procedures

    thanks for the help
    the quantity =
    helped alot, but know when i run my program and hit the clear button it says i cant convert from string to double,
    and the subprocedure and tax and total are all still at 0

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: hw problem functions and sub procedures

    change:
    vb Code:
    1. Function getitem()as string
    to:
    vb Code:
    1. Function getitem()as double
    &
    vb Code:
    1. return TxtItemAmount.Text
    to:
    vb Code:
    1. return cdbl(TxtItemAmount.Text)

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: hw problem functions and sub procedures

    the converting from a string to a double problem is because the string must be a number for you to convert it to a double. if the textbox is empty, it isn't a number and can't be converted to a number. one way round this is to use double tryparse:

    vb Code:
    1. Function getitem() As Double
    2.      Dim quantity As Double
    3.      If Double.TryParse(TxtQuantity.Text, quantity) Then
    4.         If Rdbcappuccino.Checked Then
    5.            TxtItemAmount.Text = 2 * quantity
    6.         ElseIf RdbEspresso.Checked Then
    7.            TxtItemAmount.Text = 2.25 * quantity
    8.         ElseIf rdbLatte.Checked Then
    9.            TxtItemAmount.Text = 1.75 * quantity
    10.         ElseIf RdbIcedLatte.Checked Then
    11.            TxtItemAmount.Text = 2.5 * quantity
    12.         ElseIf RdbIcedCappuccino.Checked Then
    13.            TxtItemAmount.Text = 2.5 * quantity
    14.         End If
    15.         Return CDbl(TxtItemAmount.Text)
    16.      else
    17.         return 0
    18.      End If
    19. End Function

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