Divil
Well-known member
- Joined
- Nov 17, 2002
- Messages
- 2,748
Ok, Ill bite. Jabe, your code is not demonstrating how objects are passed per se, its merely demonstrating out parameters. You can change the original class passed in in the latter procedure because youre passing byref, but either way youre creating a new instance so it doesnt prove much.
Take the code below, which is a cut down version of your. No matter whether the instance is passed byval or byref, its a class so youre only passing a reference and the number will always be changed.
Take the code below, which is a cut down version of your. No matter whether the instance is passed byval or byref, its a class so youre only passing a reference and the number will always be changed.
Code:
Public Class Tester
Public Shared Sub Main()
Dim t As New Tester, referenceType As New MyRefType
referenceType.RefTypeProp = 1
MessageBox.Show("Starting Value: " & referenceType.RefTypeProp.ToString())
t.PassByVal(referenceType)
MessageBox.Show("After being passed by value: " & referenceType.RefTypeProp.ToString())
t.PassByRef(referenceType)
MessageBox.Show("After being passed by reference: " & referenceType.RefTypeProp.ToString())
MessageBox.Show("Its always being changed because youre only passing a reference of the same object, NOT a copy of the object. Q.E.D.")
End Sub
Public Sub PassByVal(ByVal refType As MyRefType)
refType.RefTypeProp = 1000
End Sub
Public Sub PassByRef(ByRef reftype As MyRefType)
reftype.RefTypeProp = 9999
End Sub
End Class
Public Class MyRefType
Private m_intRefTypeProp As Integer
Public Property RefTypeProp() As Integer
Get
Return m_intRefTypeProp
End Get
Set(ByVal Value As Integer)
m_intRefTypeProp = Value
End Set
End Property
End Class