-2

I'm trying to create an Income Tax Calculator using procedures and functions. The code should be self-explanatory. Everything is returning values of zero, can someone give me a little bit of insight of where I'm going wrong?

Partial Class TaxCalcualtor
Inherits System.Web.UI.Page
Private totalWages As Decimal
Private taxesWithheld As Decimal
Private deductions As Integer
Private married As Boolean
Private taxLiability As Decimal
Private federalTaxes As Decimal
Private stateTaxes As Decimal
Private localTaxes As Decimal

Function SingleFunction(ByVal totalWages As Decimal, ByVal married As Boolean, ByVal taxLiability As Decimal) As Decimal
    Dim taxRate As Decimal
    totalWages = CDec(TextBoxWages.Text)
    If married = False And totalWages < 25000.0 Then
        taxRate = 0.15
    ElseIf married = False And totalWages >= 25000.0 And totalWages < 45000.0 Then
        taxRate = 0.18
    ElseIf married = False And totalWages >= 45000.0 And totalWages < 65000.0 Then
        taxRate = 0.22
    ElseIf married = False And totalWages >= 65000.0 And totalWages < 85000.0 Then
        taxRate = 0.28
    ElseIf married = False And totalWages >= 85000.0 And totalWages < 100000.0 Then
        taxRate = 0.32
    ElseIf married = False And totalWages >= 100000.0 Then
        taxRate = 0.35
    End If
    taxLiability = taxRate * totalWages
    Return taxLiability
End Function

Function SingleMarriedFunction(ByVal totalWages As Decimal, ByVal married As Boolean, ByVal taxLiability As Decimal) As Decimal
    Dim taxRate As Decimal
    totalWages = CDec(TextBoxWages.Text)
    If married = True And totalWages < 25000.0 Then
        taxRate = 0.13
    ElseIf married = True And totalWages >= 25000.0 And totalWages < 45000.0 Then
        taxRate = 0.16
    ElseIf married = True And totalWages >= 45000.0 And totalWages < 65000.0 Then
        taxRate = 0.18
    ElseIf married = True And totalWages >= 65000.0 And totalWages < 85000.0 Then
        taxRate = 0.2
    ElseIf married = True And totalWages >= 85000.0 And totalWages < 100000.0 Then
        taxRate = 0.22
    ElseIf married = True And totalWages >= 100000.0 Then
        taxRate = 0.24
    End If
    taxLiability = taxRate * totalWages
    Return taxLiability
End Function

Function FederalTax(ByVal taxWithheld As Decimal, ByVal taxLiability As Decimal, ByVal deductions As Integer) As Decimal
    Dim federalTaxes As Decimal
    taxesWithheld = CDec(TextBoxTaxesWithheld.Text)
    federalTaxes = taxWithheld - taxLiability - (deductions * 750)
    Return federalTaxes
End Function

Function StateTax(ByVal totalWages As Decimal) As Decimal
    Dim stateTaxes As Decimal
    totalWages = CDec(TextBoxWages.Text)
    stateTaxes = totalWages * 0.06
    Return stateTaxes
End Function

Function LocalTax(ByVal totalWages As Decimal) As Decimal
    Dim localTaxes As Decimal
    totalWages = CDec(TextBoxWages.Text)
    localTaxes = totalWages * 0.01
    Return localTaxes
End Function

Sub DisplayData()
    Label7.Text += "Tax Liability: " + CStr(taxLiability)
    Label7.Text += "Federal Taxes: " + CStr(federalTaxes)
    Label7.Text += "State Taxes: " + CStr(stateTaxes)
    Label7.Text += "Local Taxes: " + CStr(localTaxes)
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DisplayData()
End Sub
End Class
user229044
  • 232,980
  • 40
  • 330
  • 338
Bob
  • 91
  • 1
  • 1
  • 9
  • Desired Behavior: Calculate Tax Liability, Federal, State, and Local Taxes Problem: All values are returning 0 – Bob Nov 07 '17 at 04:11
  • You are displaying values that have never been initialized/modified. If there is no more code, *taxLiability* for example is never modified. The computation functions are never called. – P.Manthe Nov 07 '17 at 04:37
  • you are never calling "SingleFunction" and/or "SingleMarriedFunction". Also shouldn't be called MarriedFunction not that it changes anything. Also you shouldn't call Global variable the same name of local variable that your are passing in your function. The goal of a function is to return a value from whatever information you passed to it. You are changing taxliability at many place but it is also declared at many places. You are having confusion because of that. The computer doesn't know witch one you really want and returns you the local one. – Chillzy Nov 07 '17 at 05:03

1 Answers1

1

There you go. Look at what I did so you can understand what is the difference from mine and yours. I also assumed you have a textbox called TextBoxDeductions. You should also have a CheckBox to click if married or not. This CheckBox I called CheckBoxMarried. I Combined your 2 functions SingleFunction and SingleMarriedFunction. Hope it makes sence for you!

   Function GetTaxLiability(ByVal totalWages As Decimal, ByVal married As Boolean) As Decimal
        Dim taxRate As Decimal
        If married = True And totalWages < 25000.0 Then
            taxRate = 0.13
        ElseIf married = True And totalWages >= 25000.0 And totalWages < 45000.0 Then
            taxRate = 0.16
        ElseIf married = True And totalWages >= 45000.0 And totalWages < 65000.0 Then
            taxRate = 0.18
        ElseIf married = True And totalWages >= 65000.0 And totalWages < 85000.0 Then
            taxRate = 0.2
        ElseIf married = True And totalWages >= 85000.0 And totalWages < 100000.0 Then
            taxRate = 0.22
        ElseIf married = True And totalWages >= 100000.0 Then
            taxRate = 0.24
        End If
        If married = False And totalWages < 25000.0 Then
            taxRate = 0.15
        ElseIf married = False And totalWages >= 25000.0 And totalWages < 45000.0 Then
            taxRate = 0.18
        ElseIf married = False And totalWages >= 45000.0 And totalWages < 65000.0 Then
            taxRate = 0.22
        ElseIf married = False And totalWages >= 65000.0 And totalWages < 85000.0 Then
            taxRate = 0.28
        ElseIf married = False And totalWages >= 85000.0 And totalWages < 100000.0 Then
            taxRate = 0.32
        ElseIf married = False And totalWages >= 100000.0 Then
            taxRate = 0.35
        End If
        Return taxRate * totalWages
    End Function

    Function FederalTax(ByVal taxWithheld As Decimal, ByVal taxLiability As Decimal, ByVal deductions As Integer) As Decimal
        Return taxWithheld - taxLiability - (deductions * 750)
    End Function

    Function StateTax(ByVal totalWages As Decimal) As Decimal
        Return totalWages * 0.06
    End Function

    Function LocalTax(ByVal totalWages As Decimal) As Decimal
        Return totalWages * 0.01
    End Function

    Sub DisplayData()
        Dim TaxLiability As Decimal
        If CheckBoxMarried.Checked = True Then
            TaxLiability = GetTaxLiability(CDec(TextBoxWages.Text), True)
        Else
            TaxLiability = GetTaxLiability(CDec(TextBoxWages.Text), False)
        End If

        Label7.Text += "Tax Liability: " + CStr(TaxLiability)
        Label7.Text += "Federal Taxes: " + CStr(FederalTax(CDec(TextBoxTaxesWithheld.Text), TaxLiability, CDec(TextBoxDeductions.text)))
        Label7.Text += "State Taxes: " + CStr(StateTax(CDec(TextBoxWages.Text)))
        Label7.Text += "Local Taxes: " + CStr(LocalTax(CDec(TextBoxWages.Text)))
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DisplayData()
    End Sub
Chillzy
  • 468
  • 3
  • 9