Sorry for such a basic scoping question but I am obviously not understanding something about scoping that is very basic. I have a very simple class:
Public Class testListClass
' This just contains a single list that is set by a property or the constructor
Private classArrayList As New ArrayList()
Public Sub New(ByVal theList As ArrayList)
classArrayList = theList
End Sub
End Class
Then I have a block of code that instantiates this when I press a button passing a new testListClass object to it containing three values (1,2,3).
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Lets see if changing the arrayList results in all of the testListClass items being changed
Dim theList As New List(Of testListClass)
Dim localArrayList As New ArrayList()
localArrayList.Add(1)
localArrayList.Add(2)
localArrayList.Add(3)
theList.Add(New testListClass(localArrayList))
' This results in theList.classArrayList being cleared. Why since the parameter
' to the constructor is passed by value?
localArrayList.Clear()
localArrayList.Add(10)
localArrayList.Add(20)
theList.Add(New testListClass(localArrayList))
End Sub
After the "theList.Add(New testListClass(localArrayList))" call, theList contains one "testListClass" object which contains three values (1,2,3) just as I would expect. The following is the what I don't understand. The next call is:
localArrayList.Clear()
If I set a breakpoint here in the debugger and execute this line what I see is:
theList(0).classArrayList has now been cleared. Where before the clear() it contained three values (1,2,3), after the call to clear the locally defined arrayList, the contents of "theList(0)" have now been cleared. Why is that? I would think since the New constructor parameter is passed by value (ByVal), locally changing a container value in the calling code would have no effect on the values previously passed to another method in a different class. What obvious principal am I missing here?