Member Avatar for leyla

Hello, I'm back again with the same code however I have tried everything and I am still stuck. The message box works, and all of the values are correct but the last value. I would rather not change the message box code, but maybe just the last line where the 'Total pay:' is. As I said, all of the values are correct in the message box but the Basic hours pay will not add the Overtime hours pay for some reason.
Please help :)

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

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

Recommended Answers

All 6 Replies

You declared a bunch of doubles then you didn't use them. Your comparisons use redundant expressions in some cases and incorrect comparisons in others. Use intermediate variables to remove redundant conversions (and to allow checking in the debugger).

If chkOvertime.Checked = True           'bad  - *.Checked is already a boolean value
If chkOvertime.Checked                  'good - remove the unnecessary comparison

And txtHoursOvertime.Text = "" = False  'bad  - unnecessary comparison to boolean
And txtHoursOvertime.Text <> ""         'good - simplify comparison

And lstHourlyRate.SelectedItem = True   'bad  - *.SelectedItem is not a boolean
And lstHourlyRate.SelectedItem >= 0     'good - replace with comparison to same type (int)

And txtHoursOvertime.Text = True        'bad  - *.Text is not a boolean value
And txtHoursOvertime.Text <> ""         'good - replace with comparison to string

'use "&" to concatenate strings instead of "+"
'convert strings to numbers before doing calculations
'remove redundant conversions

If chkOvertime.Checked And txtHoursOvertime.Text <> "" And lstHourlyRate.SelectedItem >= 0 And txtHoursOvertime.Text <> "" Then

    Dim basicHours As Double = Double.Parse(txtHoursWorked.Text)
    Dim basicRate  As Double = Double.Parse(lstHourlyRate.SelectedItem)
    Dim otHours    As Double = Double.Parse(txtHoursOvertime.Text)
    Dim otRate     As Double = 2 * basicRate
    Dim basicPay   As Double = basicRate * basicHours
    Dim otPay      As Double = otRate * otHours 
    Dim totalPay   As Double = basicPay + otPay
    
    MessageBox.Show( _
        "Basic hours worked:   " & basicHours.ToString("$0.00") & vbCr & vbLf & _
        "Hourly rate of pay:   " & otRate.ToString("$0.00") & vbCr & vbLf & _
        "Basic pay:            " & basicPay.ToString("$0.00") & vbCr & vbLf & _
        "Overtime hourly rate: " & otRate.ToString("$0.00") & vbCr & vbLf & _
        "Overtime pay:         " & otPay.ToString("$0.00") & vbCr & vbLf & _
        "Total pay:            " & totalPay.ToString("$0.00") _
    )
Else
    MsgBox("You must fill everything in!")
End If

I added spaces to make the code clearer here but they will be removed by VB.

Using the intermediate values will make the output statement clearer. You can add formatting to each line to produce currency values in the proper form. And just to be picky, you won't get any output if no overtime was worked. Is that really what you want, or do you want a separate if-then-else to calculate overtime values and set the values to 0 if no ot was worked?

You are a BAD reader mate.
If you would read my post in some other tread of yours, you would see I told you you can NOT do mathematical opearations over stirngs.
Text property or textbox, SelectedItem of listBox, are type of string, and thats why you cannot do math operations over them - I repeat.

You are ONLY allowed to do math operations over real number types, like integer, decimal, double, float, uint, ulong.

So what to do? You already have examples that we gave you. Convert or Parse to particulat type.

Do:

Double.Parse(txtHoursWorked.Text) * Double.Parse(delstHourlyRate.SelectedItem) + Double.Parse(txtHoursOvertime.Text) * DoubleRate

.line.14, include those values in parentheses: "Total pay: " & (txtHoursWorked.Text * lstHourlyRate.SelectedItem + txtHoursOvertime.Text * DoubleRate)) and it should work.

codeorder: How on earth can you multiply strings together (or any other math operations)? Never seen thi before...

>>How on earth can you multiply strings together If all .Numeric, Then "why.Not?"

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.