JCS5353
JCS5353

Reputation: 13

VB Program not working, should be simple

This is a homework assignment, as far as I know I have everything I'm supposed to do. Yet it doesn't work for some reason. If I input "50000" for the Property Value, ".6" for the Assessment Percent, and ".7" for the Assessment Rate I should get "$210" for Property Tax, yet I keep getting only a zero, not sure what I missed. It's not due until Sunday, so if you only want to give me a hint that would also be appreciated. I've gone over the instructions and the code a few times and I'm still not seeing my mistake. I keep looking at "propertytaxLabel.Text = CDec(decTax)" like I messed up something there also.

Public Class Form1
Private Sub clearButton_Click(sender As Object, e As EventArgs) Handles clearButton.Click
    valueTextBox.Clear()
    percentTextBox.Clear()
    rateTextBox.Clear()
    propertytaxLabel.Text = ""
End Sub

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Dim result As DialogResult
    result = MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.YesNo)
    If result = DialogResult.No Then
        e.Cancel = True
    End If
End Sub
Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
    Try
        Dim decValue, decPercent, decRate, decTax As Decimal
        decValue = CDec(valueTextBox.Text)
        decPercent = CDec(percentTextBox.Text)
        decRate = CDec(rateTextBox.Text)

        propertytaxLabel.Text = CStr(decTax)

        If proceduresRadioButton.Checked = True Then
            TaxCalcProc(decValue, decPercent, decRate, decTax)
        Else
            decTax = TaxCalcFunc(decValue, decPercent, decRate)
        End If
        FormatCurrency(decTax)
    Catch ex As Exception
        MessageBox.Show("Error: Did you enter the data correctly?")
    End Try
End Sub
Private Sub TaxCalcProc(ByVal propVal As Decimal, ByVal perc As Decimal, ByVal rate As Decimal, ByRef tax As Decimal)
    Dim AssessedValue As Decimal
    Try
        AssessedValue = propVal * perc
        tax = AssessedValue / 100 * rate
        If rate > 1 Or perc > 1 Then
            Throw New Exception("Tax Greater Than Value. Did you use the decimal point?")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
Private Function TaxCalcFunc(ByVal propVal As Decimal, ByVal perc As Decimal, ByVal rate As Decimal) As Decimal
    Dim tax As Decimal
    Dim AssessedValue As Decimal
    Try
        AssessedValue = propVal * perc
        tax = AssessedValue / 100 * rate
        If rate > 1 Or perc > 1 Then
            Throw New Exception("Tax Greater Than Value. Did you use the decimal point?")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return 0
    End Try
    Return Tax
End Function

End Class

Upvotes: 1

Views: 60

Answers (3)

Johnie Karr
Johnie Karr

Reputation: 2822

You are printing out your decTax to your label after you are declaring it, but not after you are calculating it.

Try moving propertytaxLabel.Text = CStr(decTax) to after FormatCurrency(decTax) inside your button click event.

Upvotes: 1

Swoogan
Swoogan

Reputation: 5558

First, you should research debugging and setting breakpoints with Visual Studio. It would make solving this very easy. Barring that, you can isolate and test your function with the following:

Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
    Dim decTax As Decimal
    TaxCalcProc(50000D, 0.6D, 0.7D, decTax)
    Dim resultString = Format(decTax, "C")
    MessageBox.Show("decTax = " + resultString, "Test", MessageBoxButtons.Ok)
End Sub

Also, change:

AssessedValue / 100 * rate

to:

AssessedValue / 100D * rate 

You probably have an unintentional case to Int because of the divide by 100. 100D tells the compiler that you want a decimal number.

Upvotes: 0

AMADANON Inc.
AMADANON Inc.

Reputation: 5929

My VB is rusty, but you're not by any chance trying to convert "50,000" to a number, and it's failing because it has a comma in it? Most string -> int and string -> float conversions (in other languages) don't like commas.

Upvotes: 0

Related Questions