Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
im trying to create list of list, i want to have something like below but every time the tempList is added to myList. a strage thing happens, the ENTIRE LIST get overwritten with the new tempList and the entire myList contains the same List that are only tempList

VB
Dim MsgList As New List(Of IList(Of String))()
   Public Sub getAllDBCMsgSigs()
       'Dim d As New OpenFileDialog()
       'd.ShowDialog()
       'getMsgs(d.FileName)

       Dim line As String
       Dim fileReader As New StreamReader("1dbcMsrgSigs.txt")
       Dim tempList As New List(Of String)
       MsgList.Add(tempList)
       Do While fileReader.Peek() > 0
           line = fileReader.ReadLine

           If line.Length > 0 Then
               tempList.Add(line)
           Else
               MsgList.Add(tempList)
               tempList.Clear()
           End If
       Loop
   End Sub


could you please tell me what im doing wrong

thanks in advance
Posted

You need to move the
VB
Dim tempList As New List(Of String)
line inside your loop.
At the moment, you are adding the same list into MsgList each time, and them clearing all the entries.
If you want a list of lists of strings, you must create a new one each time.

I don't know why you are doing it that way though - it seems a very long way round...
 
Share this answer
 
yes, so i create a list of string in my tempList, then when the list is done i put the entire list into myList, then I start again creating a tempList of strings.

simply im creating a 2D array of strings

i just dont understand why it is oferwriting my entire myList.
once the tempList has been added to myList.add..(i.e. myList(0) get tempList) for exanmple
it should not be able to oversrite it unless i say myList0)=tempList
 
Share this answer
 
v2
Comments
Nelek 22-Apr-12 18:19pm    
This doesn't seem to be an answer. If you wanted to speak with OriginalGriff you should use the button "have a question or comment?" below his message (as I am doing right now with you).
Use Activator.CreateInstance(Of List(Of String)) i.e.
VB
Dim outerList as List(Of List(Of String))

For i as Integer = 0 to 1000
    outerList.Add(Activator.CreateInstance(Of List(Of String)))
Next

This creates outerList with 1000 anonymous Lists(Of String),
each being only accessible by their index in outerList.

See: http://msdn.microsoft.com/en-us/library/system.activator.createinstance(v=vs.100).aspx[^]
 
Share this answer
 
Comments
Ron Beyer 9-Sep-13 0:06am    
This is just about the worst way to do this. This is an abuse of Activator.CreateInstance and not its intended use. Not only is this a HUGE bottleneck, but its completely unnecessary.
jediYL 24-Sep-13 22:49pm    
Thanks Ron. Somethings of Microsoft's dynamic platform seem to me a sort of personal schism, that would be appropriate for a company of the size of DazStudio and not the many thousandth developer that works here. One is the need for the user to have to come into the possession of an object on a level higher than the level that substantiates every method of its existence, for instance, a list that furthermore needs a NaMe. Likewise, the absence of a deep copy leaves many scratching their head (for some relief from the pain, no doubt.)
But if you hold the solution that solves this riddle, relinquish it. How does one create "nameless" lists the way one instantiates buttons, for instance, as the children of a form or control? Zany.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900