[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
Re: Question about re-initializing List box
What does your Add() method look like?
Re: Question about re-initializing List box
Quote:
Originally Posted by
kleinma
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
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.
Re: Question about re-initializing List box
Quote:
Originally Posted by
Shaggy Hiker
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.