Somewhat unclear to me are references (pointers?) to classes in VB.NET. The question I am about to ask can be answered by a little bit of testing, but I was wondering if anybody could post a decent explanation (or links, too).
If you create a class:
Public Class ReferenceClass Private myBooleanValue As Boolean = False Public Property BooleanValue As Boolean Get Return myBooleanValue End Get Set(value As Boolean) myBooleanValue = value End Set End Property End Class
And then a class which actually uses this class as a property:
Public Class UsingClass Private myReference As ReferenceClass Public Property Reference As ReferenceClass Get return myReference End Get Set(value As ReferenceClass) myReference = value End Set End Property Public Sub New(ByVal Reference As ReferenceClass) myReference = Reference End Sub End Class
And then use it like this:
Public Class RuntimeOrSomething Public Shared myReference As ReferenceClass Public Shared ReadOnly Property Reference As ReferenceClass Get If myReference Is Nothing Then myReference = new ReferenceClass() return myReference End Get End Property Public Shared Function BooleanCheck() As Boolean Reference.BooleanValue = True Dim tempClass As New UsingClass(Reference) tempClass.Reference.BooleanValue = False Return (tempClass.Reference.BooleanValue = Reference.BooleanValue) End Sub Public Shared Sub DoNothing() Reference.BooleanValue = True Dim someBoolean As Boolean = BooleanCheck ' Now Reference.Booleanvalue is 'False' End Sub End Class
Now the function BooleanCheck will always return true, even though the reference is passed to the new class UsingClass ‘by value’, not by reference. So a copy of the class is not made, but the local variable myReference in UsingClass still references/points to the property Reference in RuntimeOrSomething.
How can this be explained elegantly?
A reference points to an instance of an object, it is not an instance of an object. Making a copy of the directions to the object does not create another object, it creates another reference that also points to the same object.