I designed a payroll application and I keep getting the error conversion from st
ID: 3626960 • Letter: I
Question
I designed a payroll application and I keep getting the error conversion from string to type Integer is not valid.Also when I enter the name in one of the textboxes it reads form and not my name. I am not sure what to do to correct this....any help would be greatly appreciated !
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim intcount As Integer = 0 'To hold counter variable.
Dim dblgrosspay As Double 'To hold Grosspay.
Dim dblstateIncome As Double 'To hold state income tax.
Dim dblfederalIncome As Double 'To Federal income tax.
Dim dblfica, fciaIn As Double 'To hold FICA.
Dim intnetPay As Integer 'To hold NetPay.
Dim strname As String 'To hold Employee name.
Dim dblstate As Double 'To hold state income tax.
Dim dblfederal As Double 'To hold federal income tax.
Dim inthour As Integer 'To hold hours.
Dim inthourRate As Integer 'To hold emplyee rate.
lblOutput.Items.Add("Employee Name Gross Pay State Income Tax Federal Income Tax FICA Net Pay")
Do While intcount < 4
'Getting input.
strname = InputBox("Enter Name:")
inthour = InputBox("Enter Number of hours:")
inthourRate = InputBox("Enter hour rate:")
dblstate = InputBox("Enter State income tax:")
dblfederal = InputBox("Enter Federal income tax:")
fciaIn = InputBox("Enter FICA:")
'Calculate grosspay, stateIncome, federalIncome, fica and netpay.
dblgrosspay = inthour * inthourRate
dblstateIncome = dblgrosspay * dblstate
dblfederalIncome = dblgrosspay * dblfederal
dblfica = dblgrosspay * fciaIn
intnetPay = dblgrosspay - (dblstate + dblfederal + fciaIn)
If (dblstate + dblfederal + fciaIn) > dblgrosspay Then
MessageBox.Show("With holdings are too high")
End If
'Display output.
lblOutput.Items.Add(Name.ToString + " " +
dblgrosspay.ToString + " " +
dblstateIncome.ToString + " " +
dblfederalIncome.ToString + " " +
dblfica.ToString + " " +
intnetPay.ToString)
intcount += 1
Loop
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Closes the application
Me.Close()
End Sub
End Class
Explanation / Answer
Dear... Below is the modified code of your code: Public Class Form1 Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim intcount As Integer = 0 'To hold counter variable. Dim dblgrosspay As Double 'To hold Grosspay. Dim dblstateIncome As Double 'To hold state income tax. Dim dblfederalIncome As Double 'To Federal income tax. Dim dblfica, fciaIn As Double 'To hold FICA. Dim intnetPay As Integer 'To hold NetPay. Dim strname As String 'To hold Employee name. Dim dblstate As Double 'To hold state income tax. Dim dblfederal As Double 'To hold federal income tax. Dim inthour As Integer 'To hold hours. Dim inthourRate As Integer 'To hold emplyee rate. lblOutput.Items.Add("Employee Name Gross Pay State Income Tax Federal Income Tax FICA Net Pay") Do While intcount < 4 'Getting input. strname = InputBox("Enter Name:", "Enter Input") inthour = CInt(InputBox("Enter Number of hours:", "Enter Input", "0")) inthourRate = CInt(InputBox("Enter hour rate:", "Enter Input", "0")) dblstate = CDbl(InputBox("Enter State income tax:", "Enter Input", "0.0")) dblfederal = CDbl(InputBox("Enter Federal income tax:", "Enter Input", "0.0")) fciaIn = CDbl(InputBox("Enter FICA:", "Enter Input", "0.0")) 'Calculate grosspay, stateIncome, federalIncome, fica and netpay. dblgrosspay = inthour * inthourRate dblstateIncome = dblgrosspay * dblstate dblfederalIncome = dblgrosspay * dblfederal dblfica = dblgrosspay * fciaIn intnetPay = dblgrosspay - (dblstate + dblfederal + fciaIn) If (dblstate + dblfederal + fciaIn) > dblgrosspay Then MessageBox.Show("With holdings are too high") End If 'Display output. lblOutput.Items.Add(strname.ToString + vbTab + dblgrosspay.ToString + vbTab + dblstateIncome.ToString + vbTab + dblfederalIncome.ToString + vbTab + dblfica.ToString + vbTab + intnetPay.ToString) intcount += 1 Loop End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'Closes the application Me.Close() End Sub End Class in this code inthour = CInt(InputBox("Enter Number of hours:", "Enter Input", "0")), the third option "0" is the default value, the value 0 is taken if enter is pressed.CInt converts the string to integer. You may get the error if you press cancel or escape button, for that you have to write a code for handling cancel button, which will be complicated. For name use strname.ToString instead Name.tostringRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.