Question

I am trying to use call statements in Visual Studio and having some issues understanding the logic. I am trying to create a windows form program to calculate "Net Pay" in a payroll situation. Just looking for some insight on how these statements work...

I am mostly just confused on how to calculate the Overtime pay portion and if there is no Overtime, then just Regular pay...

This is what I have so far:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim hours As Double
    Dim rate As Double
    Dim regpay As Double
    Dim overpay As Double

    hours = TextBox5.Text
    rate = TextBox4.Text

    If hours > 40 Then
        Regular(regpay)
        Overtime(overpay)
    Else
        Regular(regpay)
    End If

End Sub

Sub Regular(ByRef regpay As Double)

End Sub

Sub Overtime()

End Sub

I'm sure it's simple, I'm just having a hard time understanding the logic and syntax.

Was it helpful?

Solution

I'd recommend defining Regular and Overtime as functions that return the computed values, i.e:

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click
    Dim hours As Double
    Dim rate As Double
    Dim regpay As Double
    Dim overpay As Double

    hours = TextBox5.Text
    rate = TextBox4.Text

    If hours > 40 Then
        regpay = Regular(40, rate)
        overpay = Overtime(hours - 40, rate)
    Else
        regpay = Regular(hours, rate)
    End If

    Dim netPay = regPay + overPay
    ' TODO: write netPay back to the form
End Sub

Function Regular(hours As Double, rate As Double) As Double
    Return hours * rate
End Function

Function Overtime(hours As Double, rate As Double) As Double
    Return hours * rate ' Note overtime may be paid at a different rate
End Function

Also for accuracy in financial calculations I'd recommend using the decimal type over double.

OTHER TIPS

You really only need one method to do this. As a quick point of order before I continue, pretty much any time you're working with money you should use the Decimal type instead of Double:

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click

    Dim hours As Decimal = Decimal.Parse(TextBox5.Text)
    Dim rate As Decimal = Decimal.Parse(TextBox4.Text)   
    Dim regpay As Decimal 
    Dim overpay As Decimal 

    If hours > 40D Then
        regpay = CalculatePay(40D, rate)
        overpay = CalculatePay(hours - 40D, rate * 1.5D)
    Else
        regpay = CalculatePay(hours, rate)
        overpay = 0D
    End If

End Sub

Function CalculatePay(ByVal hours As Decimal, ByVal rate As Decimal) As Decimal
    Return hours * rate
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top