Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. List of List returning only copies of last list added

List of List returning only copies of last list added

Scheduled Pinned Locked Moved Visual Basic
databasedata-structuresjsontutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BubbaBeans
    wrote on last edited by
    #1

    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.

    E D 2 Replies Last reply
    0
    • B BubbaBeans

      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.

      E Offline
      E Offline
      Estys
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • B BubbaBeans

        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.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • E Estys

          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

          B Offline
          B Offline
          BubbaBeans
          wrote on last edited by
          #4

          Dang it... I KNEW that, too! I guess it is time to start drinking coffee again. Thanks to both of you!:thumbsup:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups