Visual Pony Coursework

Post » Mon May 14, 2012 9:27 am

Hey so I decided to post this here because I feel these forums have the best off topic community and have a much faster response than most others (that I've signed up to)

So I'm trying to create a program in Visual Basic for my uni coursework and I'm kinda stumped a tad here. I've created most of it, I just need to write the files to a txt file (in the same folder as the program [bin/debug]).

I'm creating a some kind of employee reference form. Fill in your details, figures out gross pay, net pay and all the taxes an employer has to deal with. When you press Save, the details should all write to a file and I should be able to select from multiple ID's in my combobox for the employee ID reference. If I try to save a file with the same ID number I should get thrown an error. every ID should be saved under the Employee ID box.

Here's my code for the save button:

   Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click		Dim s As employee = New employee		s.EmployeeNo = cboEmployeeNo.Text		s.Name = ("txtName.Text")		s.HourlyRate = txtHourlyRate.Text		s.HoursWorked = txtHoursWorked.Text		s.DOB = dtpDOB.Text		s.NatIns = ("txtNatIns.Text")		s.NetPay = txtNetPay.Text		s.GrossPay = txtGrossPay.Text		s.Tax = txtTax.Text		s.NatInsContrib = txtNatInsContrib.Text		employees.Add(s)		cboEmployeeNo.Items.Clear()		For Each item In employees			cboEmployeeNo.Items.Add(item.Name)		Next		MsgBox("Text Appended to the File")	End Sub
Which throws me the following error: "Conversion from string "txtName.Text" to type 'Integer' is not valid."

I don't know how to specify the entries as text rather than integers.



And if you need it for reference, here's the whole code:
(It's not much, is just a class in the basics)

Spoiler
Public Class txtEmployeeNo	Dim fn As String = "employees.txt"	Structure employee		Dim EmployeeNo As String		Dim Name As Integer		Dim HourlyRate As String		Dim HoursWorked As String		Dim DOB As String		Dim NatIns As String		Dim NetPay As String		Dim GrossPay As String		Dim Tax As String		Dim NatInsContrib As String	End Structure	Dim employees As List(Of employee) = New List(Of employee)	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click		Dim currentDate As Date = Today		Dim birthday As Date = dtpDOB.Value.Date		Dim Age As Integer = currentDate.Year - birthday.Year		Try			txtGrossPay.Text = txtHourlyRate.Text * txtHoursWorked.Text			txtNatInsContrib.Text = txtGrossPay.Text / 100 * 7			If Age < 18 Then				txtTax.Text = txtGrossPay.Text / 100 * 18			ElseIf Age > 60 Then				txtTax.Text = txtGrossPay.Text / 100 * 20			Else				txtTax.Text = txtGrossPay.Text / 100 * 25			End If			txtNetPay.Text = txtGrossPay.Text - txtTax.Text - txtNatInsContrib.Text		Catch ex As Exception			MessageBox.Show("You Must enter Hourly Rate and Hours Worked")		End Try	End Sub	Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click		Dim s As employee = New employee		s.EmployeeNo = cboEmployeeNo.Text		s.Name = ("txtName.Text")		s.HourlyRate = txtHourlyRate.Text		s.HoursWorked = txtHoursWorked.Text		s.DOB = dtpDOB.Text		s.NatIns = ("txtNatIns.Text")		s.NetPay = txtNetPay.Text		s.GrossPay = txtGrossPay.Text		s.Tax = txtTax.Text		s.NatInsContrib = txtNatInsContrib.Text		employees.Add(s)		cboEmployeeNo.Items.Clear()		For Each item In employees			cboEmployeeNo.Items.Add(item.Name)		Next		MsgBox("Text Appended to the File")	End Sub	Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click		Dim response As MsgBoxResult		response = MsgBox("Exit Form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")		If response = MsgBoxResult.Yes Then			Me.Dispose()		ElseIf response = MsgBoxResult.No Then			Exit Sub		End If	End SubEnd Class


thanks for any help. :smile:
User avatar
Ownie Zuliana
 
Posts: 3375
Joined: Thu Jun 15, 2006 4:31 am

Post » Mon May 14, 2012 3:58 pm

Public Class txtEmployeeNo
Dim fn As String = "employees.txt"

Structure employee
Dim EmployeeNo As String
Dim Name As Integer
Dim HourlyRate As String
Dim HoursWorked As String
Dim DOB As String
Dim NatIns As String
Dim NetPay As String
Dim GrossPay As String
Dim Tax As String
Dim NatInsContrib As String
End Structure
Dim employees As List(Of employee) = New List(Of employee)
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim currentDate As Date = Today
Dim birthday As Date = dtpDOB.Value.Date
Dim Age As Integer = currentDate.Year - birthday.Year
Try
txtGrossPay.Text = txtHourlyRate.Text * txtHoursWorked.Text
txtNatInsContrib.Text = txtGrossPay.Text / 100 * 7
If Age < 18 Then
txtTax.Text = txtGrossPay.Text / 100 * 18
ElseIf Age > 60 Then
txtTax.Text = txtGrossPay.Text / 100 * 20
Else
txtTax.Text = txtGrossPay.Text / 100 * 25
End If
txtNetPay.Text = txtGrossPay.Text - txtTax.Text - txtNatInsContrib.Text
Catch ex As Exception
MessageBox.Show("You Must enter Hourly Rate and Hours Worked")
End Try
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim s As employee = New employee
s.EmployeeNo = cboEmployeeNo.Text
s.Name = ("txtName.Text")
s.HourlyRate = txtHourlyRate.Text
s.HoursWorked = txtHoursWorked.Text
s.DOB = dtpDOB.Text
s.NatIns = ("txtNatIns.Text")
s.NetPay = txtNetPay.Text
s.GrossPay = txtGrossPay.Text
s.Tax = txtTax.Text
s.NatInsContrib = txtNatInsContrib.Text
employees.Add(s)
cboEmployeeNo.Items.Clear()
For Each item In employees
cboEmployeeNo.Items.Add(item.Name)

Next
MsgBox("Text Appended to the File")
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Dim response As MsgBoxResult
response = MsgBox("Exit Form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
If response = MsgBoxResult.Yes Then
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
Exit Sub
End If
End Sub
End Class



thanks for any help. :smile:

Did only a little programming and seemingly not in the same way as you do but could the red underlined line have something to do with this error ? Kinda looks like you defined name as integer but everything else as string.
User avatar
Thema
 
Posts: 3461
Joined: Thu Sep 21, 2006 2:36 am

Post » Mon May 14, 2012 8:16 pm

You seemed to have reversed the data types on EmployeeNo and Name. If you swap those around it should work.
User avatar
Roberto Gaeta
 
Posts: 3451
Joined: Tue Nov 06, 2007 2:23 am


Return to Othor Games