I am trying to catch the System.InvalidCastException error. If I put a number into the calculator the program runs ok. If the calculate button is pressed with nothing in the text boxes I get the casting error Conversion from string "" to type 'Decimal' is not valid. I understand why I am getting the error. I don't know what to do about it. I want the program to dump null data and go back to waiting for input from the user. Thanks
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim FedTaxRate = 0.13 ' constants for taxes and work week
Dim StateTaxRate = 0.07
Dim StandWorkWeek = 40
Dim GrossPay As Decimal ' variables
Dim NetPay As Decimal
If txtInHours.Text = "" Then
MessageBox.Show("Please enter a number in the hours box", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
If txtInWage.Text = "" Then
MessageBox.Show("Please enter a number in the wage box", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
End If
End If
Dim decInHours = CDec(txtInHours.Text) 'converts text boxes to numerical data
Dim decInWage = CDec(txtInWage.Text)
If decInHours <= StandWorkWeek Then 'calculates gross and net pay as well as taxes. Also includes overtime after 40 hours
GrossPay = (decInHours * decInWage)
ElseIf decInHours > StandWorkWeek Then
GrossPay = (decInWage * StandWorkWeek) + (decInHours - StandWorkWeek) * (decInWage * 1.5)
End If
NetPay = GrossPay - (GrossPay * FedTaxRate) - (GrossPay * StateTaxRate)
lblGrossPay.Text = GrossPay.ToString("c")
lblNetPay.Text = NetPay.ToString("c")
lblFedTax.Text = (GrossPay * FedTaxRate).ToString("c")
lblStateTax.Text = (GrossPay * StateTaxRate).ToString("C")
End Sub