List of List returning only copies of last list added
-
I am attempting to create a list of lists so I can return a specific list when I reference list(index), for example. I created a custom class and am using it as the list type. A subroutine reads through a text file (csv) one line at a time. The value in the line indicates the "section number" to which the rest of the line belongs, and all other values on that line are used to set certain properties in my custom class. (Confusing?) Basically it reads through the lines, and if the first number is the same as the first number in the previous line, it processes the line and adds it to a list. If the number is different, it adds the list to the main list, then clears the list, and adds the current line to the new list. And repeat until the end of the file. Here is the code:
Dim EvalTopics As new List(of List(of customClass))
Dim sr As StreamReader = New StreamReader(BLAHBLAHBLAH)
Dim OldSec As Integer = 0, NewSec As Integer = 0 'Old and New Section Numbers
Dim seclist As New List(Of customClass) 'List to be added to EvalTopics
While Not sr.EndOfStream
Dim splits() As String = sr.Readline.Split(",")
NewSec = splits(0) 'Section # is first value
If OldSec <> NewSec Then
EvalTopics.Add(seclist)
seclist.Clear()
OldSec = NewSec
End If
Dim es As customClass = New customClass
With es
'assign the other values to various properties
End With
seclist.Add(es)
End While
EvalTopics.Add(seclist)'Add the last section
sr.Close()After populating the lists, if I call up EvalTopics(0), I get the same as EvalTopics(3). In fact, every list I call up is the same as the LAST LIST ADDED. Can someone point me in the right direction? At first I thought I was using lists incorrectly, so I tried an array of lists, then a list of arrays. Nothing worked.
-
I am attempting to create a list of lists so I can return a specific list when I reference list(index), for example. I created a custom class and am using it as the list type. A subroutine reads through a text file (csv) one line at a time. The value in the line indicates the "section number" to which the rest of the line belongs, and all other values on that line are used to set certain properties in my custom class. (Confusing?) Basically it reads through the lines, and if the first number is the same as the first number in the previous line, it processes the line and adds it to a list. If the number is different, it adds the list to the main list, then clears the list, and adds the current line to the new list. And repeat until the end of the file. Here is the code:
Dim EvalTopics As new List(of List(of customClass))
Dim sr As StreamReader = New StreamReader(BLAHBLAHBLAH)
Dim OldSec As Integer = 0, NewSec As Integer = 0 'Old and New Section Numbers
Dim seclist As New List(Of customClass) 'List to be added to EvalTopics
While Not sr.EndOfStream
Dim splits() As String = sr.Readline.Split(",")
NewSec = splits(0) 'Section # is first value
If OldSec <> NewSec Then
EvalTopics.Add(seclist)
seclist.Clear()
OldSec = NewSec
End If
Dim es As customClass = New customClass
With es
'assign the other values to various properties
End With
seclist.Add(es)
End While
EvalTopics.Add(seclist)'Add the last section
sr.Close()After populating the lists, if I call up EvalTopics(0), I get the same as EvalTopics(3). In fact, every list I call up is the same as the LAST LIST ADDED. Can someone point me in the right direction? At first I thought I was using lists incorrectly, so I tried an array of lists, then a list of arrays. Nothing worked.
-
I am attempting to create a list of lists so I can return a specific list when I reference list(index), for example. I created a custom class and am using it as the list type. A subroutine reads through a text file (csv) one line at a time. The value in the line indicates the "section number" to which the rest of the line belongs, and all other values on that line are used to set certain properties in my custom class. (Confusing?) Basically it reads through the lines, and if the first number is the same as the first number in the previous line, it processes the line and adds it to a list. If the number is different, it adds the list to the main list, then clears the list, and adds the current line to the new list. And repeat until the end of the file. Here is the code:
Dim EvalTopics As new List(of List(of customClass))
Dim sr As StreamReader = New StreamReader(BLAHBLAHBLAH)
Dim OldSec As Integer = 0, NewSec As Integer = 0 'Old and New Section Numbers
Dim seclist As New List(Of customClass) 'List to be added to EvalTopics
While Not sr.EndOfStream
Dim splits() As String = sr.Readline.Split(",")
NewSec = splits(0) 'Section # is first value
If OldSec <> NewSec Then
EvalTopics.Add(seclist)
seclist.Clear()
OldSec = NewSec
End If
Dim es As customClass = New customClass
With es
'assign the other values to various properties
End With
seclist.Add(es)
End While
EvalTopics.Add(seclist)'Add the last section
sr.Close()After populating the lists, if I call up EvalTopics(0), I get the same as EvalTopics(3). In fact, every list I call up is the same as the LAST LIST ADDED. Can someone point me in the right direction? At first I thought I was using lists incorrectly, so I tried an array of lists, then a list of arrays. Nothing worked.
You don't get copies of Lists, or any other reference type, without explicitly making the copies. It's never done implicitly.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Instead of
seclist.Clear()
use
seclist = New List(Of customClass)
Adding seclist to Evaltopics adds a reference, not a copy. So it's necessary to create a new List after adding. Cheers
Dang it... I KNEW that, too! I guess it is time to start drinking coffee again. Thanks to both of you!:thumbsup: