I am trying to populate an array list from a fixed length file. The code processes through the file ok, but my arraylist ends up with 17 copies (the number of records in the file) of the last record? Her is the code
Public Sub LoadMachineArray()
Dim NewMachine As New Machine
Dim sr As StreamReader
Dim LineData As String
MachineList.Clear()
sr = New StreamReader("c:\SupplyData\Machines.txt")
LineData = sr.ReadLine
Do While (Not LineData Is Nothing)
NewMachine.MachineId = Mid(LineData, 1, 19).Trim
NewMachine.Type = Mid(LineData, 20, 19).Trim
NewMachine.Supply(0) = Mid(LineData, 40, 19).Trim
NewMachine.Supply(1) = Mid(LineData, 60, 19).Trim
NewMachine.Supply(2) = Mid(LineData, 80, 19).Trim
NewMachine.Supply(3) = Mid(LineData, 100, 19).Trim
NewMachine.Supply(4) = Mid(LineData, 120, 19).Trim
NewMachine.Supply(5) = Mid(LineData, 140, 19).Trim
NewMachine.Supply(6) = Mid(LineData, 160, 19).Trim
NewMachine.Supply(7) = Mid(LineData, 180, 19).Trim
NewMachine.Supply(8) = Mid(LineData, 200, 19).Trim
NewMachine.Supply(9) = Mid(LineData, 220, 19).Trim
NewMachine.Supply(10) = Mid(LineData, 240, 29).Trim
MachineList.Add(NewMachine)
LineData = sr.ReadLine
Loop
'NewMachine = Nothing
For Each mach As Machine In MachineList
ListBox1.Items.Add(mach.MachineId & "-" & mach.Type & "-" & mach.Supply(0))
Next
End Sub
The list box (listbox1) ends up with 17 copies of the last record?
Public Sub LoadMachineArray()
Dim NewMachine As New Machine
Dim sr As StreamReader
Dim LineData As String
MachineList.Clear()
sr = New StreamReader("c:\SupplyData\Machines.txt")
LineData = sr.ReadLine
Do While (Not LineData Is Nothing)
NewMachine.MachineId = Mid(LineData, 1, 19).Trim
NewMachine.Type = Mid(LineData, 20, 19).Trim
NewMachine.Supply(0) = Mid(LineData, 40, 19).Trim
NewMachine.Supply(1) = Mid(LineData, 60, 19).Trim
NewMachine.Supply(2) = Mid(LineData, 80, 19).Trim
NewMachine.Supply(3) = Mid(LineData, 100, 19).Trim
NewMachine.Supply(4) = Mid(LineData, 120, 19).Trim
NewMachine.Supply(5) = Mid(LineData, 140, 19).Trim
NewMachine.Supply(6) = Mid(LineData, 160, 19).Trim
NewMachine.Supply(7) = Mid(LineData, 180, 19).Trim
NewMachine.Supply(8) = Mid(LineData, 200, 19).Trim
NewMachine.Supply(9) = Mid(LineData, 220, 19).Trim
NewMachine.Supply(10) = Mid(LineData, 240, 29).Trim
MachineList.Add(NewMachine)
LineData = sr.ReadLine
Loop
'NewMachine = Nothing
For Each mach As Machine In MachineList
ListBox1.Items.Add(mach.MachineId & "-" & mach.Type & "-" & mach.Supply(0))
Next
End Sub
The list box (listbox1) ends up with 17 copies of the last record?