Results 1 to 5 of 5

Thread: [RESOLVED] Question about re-initializing List box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Resolved [RESOLVED] Question about re-initializing List box

    Why if I run the following code does the argument in the Add method pass {"foo2", "bar2"} in both method calls?

    Code:
            Dim myList As New List(Of String)
    
            myList.AddRange(New String() {"foo", "bar"})
            Add(New RandomClass("blah", myList ))
    
            myList .Clear()
            lb.AddRange(New String() {"foo2", "bar2"})
            Add(New RandomClass("blah2", myList ))
    Thanks

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Question about re-initializing List box

    What does your Add() method look like?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: Question about re-initializing List box

    Quote Originally Posted by kleinma View Post
    What does your Add() method look like?
    Here's the revised code:
    Code:
        Dim myList As New List(Of String)
    
        myList.AddRange(New String() {"foo", "bar"})
        Add(New InherittingClass1("blah", myList ))
    
        myList.Clear()
        lb.AddRange(New String() {"foo2", "bar2"})
        Add(New InherittingClass2("blah2", myList ))

    Code:
        Public Function Add(ByVal Item As BaseClass) As Integer
            m_List.Add(Item)   'm_list is As List(Of BaseClass) attribute  
            Return m_List.Count
        End Function

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Question about re-initializing List box

    It doesn't. How are you coming to the conclusion that it does? Are you looking at something in your RandomClass to see that the values are the same? If so, the problem has nothing to do with how you load the list, the problem is that you are passing the exact same list to both constructors.

    When you pass a reference type (anything other than a number, practically, though there are a few others), all you are really passing is the address of the object. Therefore, both of your InheritingClass instances are being passed the exact same thing: The address of your list. They aren't getting copies of the list, they are just getting the address. What this means is that you are setting the list with one set of values, passing the address to the first instance, then clearing the list and repeating the steps with new values. Since both instances of the InheritingClass have the address of the object, when you clear the list, you clear it for both objects, because there is only one list, not a different one in each InheritingClass.

    This is something known as a Shallow Copy, and it is the only way that reference types can be made to work automatically. If a copy of the list was being passed, that would be a Deep Copy, but no compiler can automatically generate the code to perform a Deep Copy such that it will work in all scenarios, so they don't exist unless you write them.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: Question about re-initializing List box

    Quote Originally Posted by Shaggy Hiker View Post
    It doesn't. How are you coming to the conclusion that it does? Are you looking at something in your RandomClass to see that the values are the same? If so, the problem has nothing to do with how you load the list, the problem is that you are passing the exact same list to both constructors.

    When you pass a reference type (anything other than a number, practically, though there are a few others), all you are really passing is the address of the object. Therefore, both of your InheritingClass instances are being passed the exact same thing: The address of your list. They aren't getting copies of the list, they are just getting the address. What this means is that you are setting the list with one set of values, passing the address to the first instance, then clearing the list and repeating the steps with new values. Since both instances of the InheritingClass have the address of the object, when you clear the list, you clear it for both objects, because there is only one list, not a different one in each InheritingClass.

    This is something known as a Shallow Copy, and it is the only way that reference types can be made to work automatically. If a copy of the list was being passed, that would be a Deep Copy, but no compiler can automatically generate the code to perform a Deep Copy such that it will work in all scenarios, so they don't exist unless you write them.
    That's a great explanation, thanks you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width