Mohamed Kamal Mohamed Kamal - 1 year ago 117 Question

unusual code processing

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

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)
StrcCurrentRow.TagName = arrCurrentRow(FolderIndex)
StrcCurrentRow.SAPFL = arrCurrentRow(FolderIndex + 1)
StrcCurrentRow.SAPEqNO = arrCurrentRow(FolderIndex + 2)
Error handling
End If

End While
PublicDatabaseList = tempList
Return Nothing

End Function

Answer Source

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)

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download