help with reading from a comma delimited file
ok heres the issue I'm attempting to read from a comma delimited file, the values are put into a structure (employeerecords) i want to know how i can access the separate sections of this structure i will provide my code so far, i apologize for the comments I'm making sure i know whats going on, I'm new to this, the code currently just dumps the whole file into a textbox, i would like to be able to access and manipulate the individual fields but i am at a genuine loss.
Thanks
Code:
'structure for the array, the values are declared in the order they appear more below.
Structure EmployeeRecords
Dim surname As String 'as first field before comma is the surname
Dim firstname As String 'as second field after surname is firstname
Dim nationalininsurance As String 'etc.
Dim taxcode As String
Dim hoursworked As Integer
Dim rateofpay As Integer
End Structure
Dim employeearray As EmployeeRecords ' stating that employeearray is now the structure employee records
Dim currentline As String
' this is the name i have decided to give to the variable where the line which is taken from the file is stored
'to be later used to input said line into a text box
Private Sub inputfile()
FileOpen(1, "C:\Users\Alex\Documents\Visual Studio 2008\Employee Doc.txt", OpenMode.Input)
Do While Not EOF(1)
currentline = LineInput(1)
TextBox1.Text = TextBox1.Text & currentline & vbNewLine
Loop
End Sub
Re: help with reading from a comma delimited file
You posted in the wrong forum. This is for VB6 and your code is .Net.
Secondly, if you have multiple employee records in the file (in new lines) you need to create an array of the EmployeeRecord structure type.
In a structure, you access the properties using . like with all other objects.
Dim employeerec As EmployeeRecords
MsgBox employeerec.surname
Re: help with reading from a comma delimited file
Thank you so much gah i can't believe i didn't think of that and i'm terribly sorry for posting in the wrong area, i'm new here .net, i will remember thank you for your advice :D
Re: help with reading from a comma delimited file
No problem. I'll tell one of the Mods to move the thread to the .Net forum.
Re: help with reading from a comma delimited file
Re: help with reading from a comma delimited file
Get rid of that VB6-esque file I/O for a start. Generally you should use types from the System.IO namespace for file I/O. In this case though, I'd suggest using the TextFieldParser, which is specifically designed for reading delimited and fixed-width text files. The MSDN documentation provides code examples.