Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB .NET list
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()
        '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 22-Apr-12 5:02am
Rate this: bad
good
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...
  Permalink  
Rate this: bad
good
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
  Permalink  
v2
Comments
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
good
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)))
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[^]
  Permalink  
Comments
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 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 8 Sep 2013
Copyright © CodeProject, 1999-2014
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