Results 1 to 4 of 4

Thread: Calculation Error.....Any ideas? [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    5

    Calculation Error.....Any ideas? [RESOLVED]

    I am taking a beginning VB.NET course, I am having trouble getting this project to calculate properly. I think the calculations are in the wrong place, cause when you uncheck a box (deselect) it continues to hold that value in memory and add it to the total. It also adds all values no matter how many check boxes you check or which discount you apply. Any help would be really appreciated!! We are required to use Option Strict in all programs for this course.

    Jennifer





    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.  
    7.  
    8. #End Region
    9.     Const mdecMANICURE_PRICE As Decimal = 35D
    10.     Const mdecMAKEOVER_PRICE As Decimal = 125D
    11.     Const mdecHAIRSTYLING_PRICE As Decimal = 60D
    12.     Const mdecPERMANENTMAKEUP_PRICE As Decimal = 200D
    13.     Const mdecDISCOUNT10 As Decimal = 0.9D
    14.     Const mdecDISCOUNT20 As Decimal = 0.8D
    15.     Dim decSubtotal As Decimal
    16.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    17.         'Exit the project
    18.         Me.Close()
    19.     End Sub
    20.     Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    21.  
    22.         If chkMakeover.Checked = True Then
    23.             lblMakeover.Text = CStr(mdecMAKEOVER_PRICE)
    24.             lblMakeover.Text = FormatCurrency(mdecMAKEOVER_PRICE)
    25.  
    26.         ElseIf chkMakeover.Checked = False Then
    27.             lblMakeover.Text = ""
    28.             decSubtotal = decSubtotal + mdecMAKEOVER_PRICE
    29.         End If
    30.  
    31.         If chkHairstyling.Checked = True Then
    32.             lblHairstyling.Text = CStr(mdecHAIRSTYLING_PRICE)
    33.             lblHairstyling.Text = FormatCurrency(mdecHAIRSTYLING_PRICE)
    34.  
    35.         ElseIf chkHairstyling.Checked = False Then
    36.             lblHairstyling.Text = ""
    37.             decSubtotal = decSubtotal + mdecHAIRSTYLING_PRICE
    38.         End If
    39.  
    40.         If chkManicure.Checked = True Then
    41.             lblManicure.Text = CStr(mdecMANICURE_PRICE)
    42.             lblManicure.Text = FormatCurrency(mdecMANICURE_PRICE)
    43.         ElseIf chkManicure.Checked = False Then
    44.             lblManicure.Text = ""
    45.             decSubtotal = decSubtotal + mdecMANICURE_PRICE
    46.         End If
    47.  
    48.         If chkPermanentmakeup.Checked = True Then
    49.             lblPermanentmakeup.Text = CStr(mdecPERMANENTMAKEUP_PRICE)
    50.             lblPermanentmakeup.Text = FormatCurrency(mdecPERMANENTMAKEUP_PRICE)
    51.         ElseIf chkPermanentmakeup.Checked = False Then
    52.             lblPermanentmakeup.Text = ""
    53.             decSubtotal = decSubtotal + mdecPERMANENTMAKEUP_PRICE
    54.         End If
    55.  
    56.         If radDiscount10.Checked = True Then
    57.             decSubtotal = decSubtotal * mdecDISCOUNT10
    58.         ElseIf radDiscount20.Checked = True Then
    59.             decSubtotal = decSubtotal * mdecDISCOUNT20
    60.         ElseIf radDiscountNo.Checked = True Then
    61.             decSubtotal = decSubtotal * 1
    62.         End If
    63.         lblTotalDue.Text = FormatCurrency(decSubtotal)
    64.  
    65.     End Sub
    66.  
    67.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    68.         If lblTotalDue.Text <> "" Then
    69.             If IsNumeric(lblTotalDue.Text) Then
    70.  
    71.                 chkMakeover.Checked = False
    72.                 chkHairstyling.Checked = False
    73.                 chkManicure.Checked = False
    74.                 chkPermanentmakeup.Checked = False
    75.                 lblTotalDue.Text = ""
    76.                 lblMakeover.Text = ""
    77.                 lblHairstyling.Text = ""
    78.                 lblManicure.Text = ""
    79.                 lblPermanentmakeup.Text = ""
    80.                 decSubtotal = 0
    81.                 radDiscount10.Checked = False
    82.                 radDiscount20.Checked = False
    83.                 radDiscountNo.Checked = False
    84.                 txtCustomerName.Clear()
    85.                 txtCustomerName.Focus()
    86.             End If
    87.  
    88.         End If
    89.     End Sub
    90. End Class
    Attached Files Attached Files
    Last edited by poodle726; Nov 30th, 2003 at 10:44 AM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Alright...

    the problem was your If ... Then statements. Also, you should have cleared decSubtotal before each Calculate .
    I highlighted the changes made in the first If - Else below. The others I changed, but didn't highlight because they follow the same logic.
    VB Code:
    1. Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    2.  
    3.         [b]decSubtotal = 0[/b]
    4.         If chkMakeover.Checked = True Then
    5.             lblMakeover.Text = CStr(mdecMAKEOVER_PRICE)
    6.             lblMakeover.Text = FormatCurrency(mdecMAKEOVER_PRICE)
    7.            
    8.            [b] decSubtotal += mdecMAKEOVER_PRICE[/b] 'add price to subtotal because box is checked
    9.  
    10.  
    11.         [b]Else 'chmakeover.checked must be false, so do not add price to decsubtotal[/b]
    12.             lblMakeover.Text = ""
    13.         End If
    14.  
    15.         If chkHairStyling.Checked = True Then
    16.             lblHairStyling.Text = CStr(mdecHAIRSTYLING_PRICE)
    17.             lblHairStyling.Text = FormatCurrency(mdecHAIRSTYLING_PRICE)
    18.             decSubtotal += mdecHAIRSTYLING_PRICE
    19.  
    20.  
    21.         Else
    22.             lblHairStyling.Text = ""
    23.         End If
    24.  
    25.         If chkManicure.Checked = True Then
    26.             lblManicure.Text = CStr(mdecMANICURE_PRICE)
    27.             lblManicure.Text = FormatCurrency(mdecMANICURE_PRICE)
    28.             decSubtotal += mdecMANICURE_PRICE
    29.         Else
    30.             lblManicure.Text = ""
    31.  
    32.         End If
    33.  
    34.         If chkPermanentMakeup.Checked = True Then
    35.             lblPermanentMakeup.Text = CStr(mdecPERMANENTMAKEUP_PRICE)
    36.             lblPermanentMakeup.Text = FormatCurrency(mdecPERMANENTMAKEUP_PRICE)
    37.             decSubtotal += mdecPERMANENTMAKEUP_PRICE
    38.         Else
    39.             lblPermanentMakeup.Text = ""
    40.  
    41.         End If
    42.  
    43.         If radDiscount10.Checked = True Then
    44.             decSubtotal = decSubtotal * mdecDISCOUNT10
    45.         ElseIf radDiscount20.Checked = True Then
    46.             decSubtotal = decSubtotal * mdecDISCOUNT20
    47.         ElseIf radDiscountNo.Checked = True Then
    48.             decSubtotal = decSubtotal * 1
    49.         End If
    50.         lblTotalDue.Text = FormatCurrency(decSubtotal)
    51.  
    52.     End Sub

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    The importance here being that when you use IF-Else:

    If checkbox is checked then
    'do something
    Else
    'obviously the checkbox is not checked
    'do something else
    End If

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    5

    Wink Re: Calculation Error....Any Ideas? [RESOLVED]

    Thanks nemaroller....worked like a charm!!! I appreciate your help!!!

    Jennifer

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