Reputation: 1
I am trying here to get the rows from the CSV file I tried it before and was no problem until now in the new project:
I am adding each row in list of structure which has (array of folders() and 3 other variables)
The problem is when i am using list.add() is working fine at the first time but for the second row when accessing the folder data from the arraycurrentrow() it is updating the previous item in the list even the code didn’t reach the list.add() part !!!!
Note: this happen only in array part but for the 3 other variables in the structure no problem
Note: i used the same code for another project but the structure hadnt any arrays ? so why the problem in the array of Folders()
I am spending all day with no hope !! If you have any idea for this problem
Public Structure DataBaseStrc
Public Property Folders As String()
Public Property TagName As String
Public Property SAPFL As String
Public Property SAPEqNO As String
End Structure
Shared Function MarafiqsDataBase_CSV()
Dim ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(DataBaseInserter.DatabaseCSVPathText.Text)
ioReader.TextFieldType = FileIO.FieldType.Delimited
ioReader.SetDelimiters(",")
Dim tempList As New List(Of DataBaseStrc)
Dim ReadingIndex As Integer
Dim StrcCurrentRow As DataBaseStrc = Nothing
Integer.TryParse(DataBaseInserter.MaxFoldersNoCB.Text, MaxFolderNo)
ReDim StrcCurrentRow.Folders(MaxFolderNo - 1)
While (Not ioReader.EndOfData)
Dim arrCurrentRow As String() = ioReader.ReadFields()
If arrCurrentRow.Length = 12 Then
Dim FolderIndex As Integer = 0
For FolderIndex = 0 To MaxFolderNo - 1
StrcCurrentRow.Folders(FolderIndex) = arrCurrentRow(FolderIndex)
Next
StrcCurrentRow.TagName = arrCurrentRow(FolderIndex)
StrcCurrentRow.SAPFL = arrCurrentRow(FolderIndex + 1)
StrcCurrentRow.SAPEqNO = arrCurrentRow(FolderIndex + 2)
tempList.Add(StrcCurrentRow)
Else
Error handling
End If
End While
PublicDatabaseList = tempList
Return Nothing
End Function
Upvotes: 0
Views: 48
Reputation: 54487
The issue looks to be the fact that you only ever create one String
array, thus you just keep overwriting the same elements every time. Inside the loop, you should be creating a new array and populating it:
StrcCurrentRow.Folders = New String(MaxFolderNo - 1) {}
For FolderIndex = 0 To MaxFolderNo - 1
StrcCurrentRow.Folders(FolderIndex) = arrCurrentRow(FolderIndex)
Next
You can get rid of the ReDim
outside the loop too.
Having said all that, I'd change that type from a structure to class and just have the class create its own array each time or perhaps even use a collection instead.
Upvotes: 1
Reputation: 2031
Move this line:
Dim StrcCurrentRow As DataBaseStrc = Nothing
Inside the while loop at its start. You are not setting it each time, thus there is only one object and you keep updating it every loop.
Upvotes: 0