Click here to Skip to main content
11,802,222 members (54,936 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB .NET
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

Dim MsgList As New List(Of IList(Of String))()
   Public Sub getAllDBCMsgSigs()
       'Dim d As New OpenFileDialog()

       Dim line As String
       Dim fileReader As New StreamReader("1dbcMsrgSigs.txt")
       Dim tempList As New List(Of String)
       Do While fileReader.Peek() > 0
           line = fileReader.ReadLine
           If line.Length > 0 Then
           End If
   End Sub

could you please tell me what im doing wrong

thanks in advance
Posted 22-Apr-12 4:02am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to move the
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...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
Nelek at 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).
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Use Activator.CreateInstance(Of List(Of String)) i.e.
Dim outerList as List(Of List(Of String))
For i as Integer = 0 to 1000
    outerList.Add(Activator.CreateInstance(Of List(Of String)))
This creates outerList with 1000 anonymous Lists(Of String),
each being only accessible by their index in outerList.

Ron Beyer at 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 at 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)

  Print Answers RSS
0 OriginalGriff 485
1 KrunalRohit 341
2 F-ES Sitecore 255
3 Afzaal Ahmad Zeeshan 220
4 CPallini 205
0 OriginalGriff 2,715
1 Maciej Los 1,900
2 KrunalRohit 1,797
3 CPallini 1,550
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 8 Sep 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100