Payroll program?

dasmin5

Member
I just wanted to know are my calculations correct?
the Fedtax is 0.06
and the stateTax is .33

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim PayRate As Double 'Employee's payrate
Dim Hours As Double 'Hours worked by employee
Dim StateTax As Double 'State deductions for employee
Dim FedTax As Double 'Federal deductions for employee
Dim GrossPay As Double 'Total Grosspay for employee
Dim NetPay As Double 'Total Netpay for employee

'Convert payrate to double
PayRate = Convert.ToDouble(Me.txtPayRate.Text)

'Convert hours to double
Hours = CDbl(Me.txtHours.Text)

'Convert statetax to double
FedTax = Convert.ToDouble(Me.txtFedTax.Text)

'Convert fedtax to double
StateTax = CDbl(Me.txtStateTax.Text)

'Determine pay
GrossPay = PayRate * Hours
NetPay = GrossPay * FedTax
NetPay = GrossPay * StateTax
NetPay = GrossPay - FedTax
NetPay = GrossPay - StateTax

'Display pay
Me.lblGrossPay.Text = Convert.ToString(GrossPay)
Me.lblNetPay.Text = Convert.ToString(NetPay)

End Sub
End Class

Paszt

Staff member
In these lines of code:
VB.NET:
``````NetPay = GrossPay * FedTax
NetPay = GrossPay * StateTax
NetPay = GrossPay - FedTax
NetPay = GrossPay - StateTax``````
you are assigning the value of NetPay four different ways. The value will always be the last operation since you aren't using the value of the first three.
Is the FedTax and StateTax percentages?

JuggaloBrotha

VB.NET Forum Moderator
Staff member
dasmin5, (i think) you're going to want a few more variables such as

VB.NET:
``````Private Const mdecFedTax as Decimal = .06
Private Const mdecStateTax as Decimal = .33

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim decPayRate As Decimal = Convert.ToDecimal(txtPayRate.Text) 'Employee's payrate
Dim intHours As Integer= Convert.ToInt32(txtHours.Text) 'Hours worked by employee
Dim decGrossPay As Decimal 'Total Grosspay for employee
Dim decNetPay As Decimal 'Total Netpay for employee
Dim decFedTaxAmt As Decimal 'Federal Tax Amount based on percentage
Dim decstateTaxAmt As Decimal 'State Tax Amount based on percentage

'Determine pay
decGrossPay = decPayRate * Convert.ToDecimal(intHours) 'Gets the total GrossPay
decFedTaxAmt = decGrossPay * mdecFedTax 'Gets the Amt of Fed Tax
decStateTaxAmt= decGrossPay * mdecStateTax 'Gets the Amt of State Tax
decNetPay = (decGrossPay - decStateTaxAmt)- decFedTaxAmt 'Gets the total NetPay

'Display pay
lblGrossPay.Text = FormatCurrency(decGrossPay)
lblNetPay.Text = FormatCurrency(decNetPay)

End Sub``````

also, i've been told whenever dealing with calculations regarding money, use the Decimal variable as it is much more accurate than Single/Double

Last edited:

dasmin5

Member

I will try and see if this will give me the result I need.

Thank you

dasmin5

Member
Help with statement?

There is a problem in my if else statement; there is a squiggly line up under the "+" and the first bracket behind that. I am adding the overtime to my work. I tried to find some help in the msdn library but, I didn't quit understand what was wrong.

Private
Const mdecFedTax As Decimal = CDec(0.06)
Private Const mdecStateTax As Decimal = CDec(0.33)

Private Const mdecMaxHours As Decimal = CDec(1.5)

Private Const mdecOverTime As Decimal = CDec(40.0)

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

Dim decPayRate As Decimal = Convert.ToDecimal(txtPayRate.Text) 'Employee's payrate

Dim intHours As Integer = Convert.ToInt32(txtHours.Text) 'Hours worked by employee

Dim decGrossPay As Decimal 'Total Grosspay for employee

Dim decNetPay As Decimal 'Total Netpay for employee

Dim decFedTaxAmt As Decimal 'Federal Tax Amount based on percentage

Dim decstateTaxAmt As Decimal 'State Tax Amount based on percentage

'Determine pay

decGrossPay = decPayRate * Convert.ToDecimal(intHours) 'Gets the total GrossPay

decFedTaxAmt = decGrossPay * mdecFedTax 'Gets the Amt of Fed Tax

decstateTaxAmt = decGrossPay * mdecStateTax 'Gets the Amt of State Tax

decNetPay = (decGrossPay - decstateTaxAmt) - decFedTaxAmt 'Gets the total NetPay

' CalcPay computes wages from the employee's pay rate

'and the hours worked, taking overtime into account

If (intHours > mdecMaxHours) Then

decGrossPay = (mdecMaxHours * decPayRate) + ' Yes

(intHours - mdecMaxHours) * decPayRate * mdecOverTime

Else

decGrossPay = intHours * decPayRate 'No

End If

'Display pay

lblGrossPay.Text = FormatCurrency(decGrossPay)

lblNetPay.Text = FormatCurrency(decNetPay)

End Sub

End
Class

JuggaloBrotha

VB.NET Forum Moderator
Staff member
VB.NET:
``````[size=2][color=#0000ff]Else

[/color][/size][size=2]decGrossPay = intHours * decPayRate [/size][size=2][color=#008000]'No

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If``````
[/color][/size]is the problem line and it's because you're trying to multiply an integer with a decimal all you need to do is convert it:
VB.NET:
``````[size=2][color=#0000ff]Else

[/color][/size][size=2]decGrossPay = Convert.ToDecimal(intHours) * decPayRate [/size][size=2][color=#008000]'No

[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2][color=#0000ff]``````

[/color][/size]and there ya go

dasmin5

Member
Thanks again

I think I am going to turn off the option strict so it can correct my mistakes for me. I am really enjoying learning this because eventually I want to create a program that will figure out "Polynomials". I am in college for Computer Science and I want to create programs that deal with math; so I have to have a full understanding of Algebra.

Paszt

Staff member
Setting Option Strict to Off is bad programming practice. That's why VS2005 sets it to On by default.
Turning it on is a performance helper by elimitating hidden type conversions. It can also catch lots of possible errors. The IDE doesn't really correct your mistakes.
If you're a computer Science major, you'll probably want to get into good programming habits.
Here's a good article that explains the best practices of the different conversion functions: Visual Basic .NET Internals. Look for a section called 'Conversion Functions, CType, DirectCast, and System.Convert'

Replies
0
Views
3K
Replies
6
Views
6K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
13
Views
2K