Member Avatar for leyla

Below is the code I have been working on in VB.NET
What basically happens is that when the user presses the calculate pay button, a message box is supposed to appear with all of the information in it, however the error comes up with: 'InvalidCastException: Conversion from string "Total pay: 5" to type 'Integer' is not valid.'

Public Class Form1
    Dim TotalPayWithoutOvertime As Integer
    Dim HoursWorked As Integer
    Dim DoubleRate As Integer
    Dim TotalPay As Integer
    Dim OvertimePay As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HoursWorked = CStr(txtHoursWorked.Text)
        TotalPayWithoutOvertime = HoursWorked * lstHourlyRate.SelectedItem
        DoubleRate = lstHourlyRate.SelectedItem * 2
        TotalPay = txtHoursWorked.Text * lstHourlyRate.SelectedItem + txtHoursOvertime.Text * DoubleRate
        OvertimePay = txtHoursOvertime.Text * DoubleRate
    End Sub
    Private Sub btnCalculatePay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculatePay.Click
        If chkOvertime.Checked Then
            lblTotal.Text = CStr(txtHoursWorked.Text * lstHourlyRate.SelectedItem + txtHoursOvertime.Text * DoubleRate)
        Else
            lblTotal.Text = txtHoursWorked.Text * lstHourlyRate.SelectedItem
        End If

        If chkOvertime.Checked = True And txtHoursOvertime.Text = "" = False And lstHourlyRate.SelectedItem = True _
            And txtHoursOvertime.Text = True Then
            MessageBox.Show("Basic hours worked: " & txtHoursWorked.Text & vbNewLine, _
                   ("Hourly rate of pay: " & lstHourlyRate.SelectedItem & vbNewLine _
                   ("Basic pay: " & txtHoursWorked.Text * lstHourlyRate.SelectedItem & vbNewLine _
                   ("Overtime hourly rate: " & DoubleRate & vbNewLine _
                   ("Overtime pay: " & OvertimePay & vbNewLine _
                   ("Total pay: " & txtHoursWorked.Text * lstHourlyRate.SelectedItem + OvertimePay))))))
        Else
            MsgBox("You must fill everything in!")
        End If


    End Sub
    Private Sub lblTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal.Click
        If chkOvertime.Checked Then
            lblTotal.Text = TotalPay
        Else
            lblTotal.Text = TotalPayWithoutOvertime
        End If

    End Sub
    Private Sub lstHourlyRate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstHourlyRate.SelectedIndexChanged
        If lstHourlyRate.SelectedItem Then
            lblTotal.Text = TotalPayWithoutOvertime
        ElseIf lstHourlyRate.SelectedItem And chkOvertime.Checked Then
            lblTotal.Text = TotalPay
        End If
    End Sub
    Private Sub chkOvertime_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOvertime.CheckedChanged
        If chkOvertime.Checked Then
            lblTotal.Text = TotalPay
        Else : lblTotal.Text = TotalPayWithoutOvertime
        End If
    End Sub
End Class

Remember:
1. you cannot do math operations over strings; you MUST convert them to appropriate type (I used integers, if trey are not, change them to appropriate type!!
2. messagebox shows strings, so if there is any value not-a-string, convert (or parse) it to stirng.
So do:

MessageBox.Show("Basic hours worked: " + txtHoursWorked.Text & vbCr & vbLf & 
"Hourly rate of pay: " & lstHourlyRate.SelectedItem.ToString() & vbCr & vbLf & 
"Basic pay: " & (Integer.Parse(txtHoursWorked.Text) * Integer.Parse(lstHourlyRate.SelectedItem.ToString())) & vbCr & vbLf & 
"Overtime hourly rate: " & DoubleRate & vbCr & vbLf & 
"Overtime pay: " & OvertimePay & vbCr & vbLf & 
"Total pay: " & (Integer.Parse(txtHoursWorked.Text) * Integer.Parse(lstHourlyRate.SelectedItem.ToString())))

If "vbCr & vbLf" dones not do, try using "Environment.NewLine"
Hope it helps,
bye

Member Avatar for leyla

Remember:
1. you cannot do math operations over strings; you MUST convert them to appropriate type (I used integers, if trey are not, change them to appropriate type!!
2. messagebox shows strings, so if there is any value not-a-string, convert (or parse) it to stirng.
So do:

MessageBox.Show("Basic hours worked: " + txtHoursWorked.Text & vbCr & vbLf & 
"Hourly rate of pay: " & lstHourlyRate.SelectedItem.ToString() & vbCr & vbLf & 
"Basic pay: " & (Integer.Parse(txtHoursWorked.Text) * Integer.Parse(lstHourlyRate.SelectedItem.ToString())) & vbCr & vbLf & 
"Overtime hourly rate: " & DoubleRate & vbCr & vbLf & 
"Overtime pay: " & OvertimePay & vbCr & vbLf & 
"Total pay: " & (Integer.Parse(txtHoursWorked.Text) * Integer.Parse(lstHourlyRate.SelectedItem.ToString())))

If "vbCr & vbLf" dones not do, try using "Environment.NewLine"
Hope it helps,
bye

Hello,
The error that I now have is: "Input string was not in a correct format.".
I am not sure how to correct this problem.

Thanks

Please try using break point, and go through the code line by line, and you will find out where exactly the problem is.
I told you maybe I didnt set the correct value, I used Integer for some of them, maybe you have some other type. Check them out!!!

If the strings you are passing into Integer.Parse contain decimal places that may be why you are getting the problem. Try using the Parse overload in this example:

MessageBox.Show(
String.Format("Basic hours worked: {0}{1}{1}Hourly rate of pay: {2}{1}{1}Basic pay: {3}{1}{1}Overtime hourly rate: {4}{1}{1}Overtime pay:{5}{1}{1}Total pay: {6}", txtHoursWorked.Text, Environment.NewLine, lstHourlyRate.SelectedItem.ToString(), Int32.Parse(txtHoursWorked.Text, System.Globalization.NumberStyles.Any) * Int32.Parse(lstHourlyRate.SelectedItem.ToString(), System.Globalization.NumberStyles.Any)), DoubleRate, OvertimePay, Int32.Parse(txtHoursWorked.Text, System.Globalization.NumberStyles.Any) * Int32.Parse(lstHourlyRate.SelectedItem.ToString(), System.Globalization.NumberStyles.Any))))

1. Do all your calculations using the appropriately typed variables?
2. Doing payroll calculations would in real life Never use integers they would be Decimal or Double types.
3. After the calculations are complete then concat your message to the user.

Dim TotalPayWithoutOvertime As Double
    Dim HoursWorked As Double
    Dim DoubleRate As Double
    Dim TotalPay As Double
    Dim OvertimePay As Double

    HoursWorked = CDbl(txtHoursWorked.Text)
    TotalPayWithoutOvertime = HoursWorked * CDbl(lstHourlyRate.SelectedItem)
    DoubleRate = CDbl(lstHourlyRate.SelectedItem) * 2
    TotalPay = CDbl(txtHoursWorked.Text) * CDbl(lstHourlyRate.SelectedItem) + CDbl(txtHoursOvertime.Text) * DoubleRate
    OvertimePay = CDbl(txtHoursOvertime.Text) * DoubleRate

The above will work provided the textboxes only accept numbers, 1 decimal point, and the negative sign. BTW I use vbCrLf all the time as it is less typing than Environment.NewLine. You also could use the System.Text.StringBuilder to create your message.

Member Avatar for leyla

Thank you for your help, everyone.
My code finally works :)

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.