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

Your code seems to present the message box saying 'please enter a value' and then continues through the rest of the function after that. It doesn't exit to await new input. Also, you've nested your if loops. txtInWage.Text = "" is only tested if txtInHours.Text = "". Meaning txtInHours needs to equal an empty string before txtInWage is checked. If txtInHours has a value entered but txtInWage doesn't you've got a problem.

ok I fixed the nested loop problem. I still have no idea how to fix the null data problem. Would I be better off using a do while loop? something like

while

textinput is not a number 

do

display error msg
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.