Public Class Tester
Public Shared Sub Main()
Dim t As New Tester(), referenceType As New MyRefType()
referenceType.RefTypeProp = 1
t.PassByVal(referenceType)
'-referenceType.RefTypeProp will revert to 1 since it was passed ByVal.
MsgBox("After PassByVal: MyRefType.RefTypeProp = " & referenceType.RefTypeProp)
t.PassByRef(referenceType)
'-referenceType.RefTypeProp will become 2000 since it was passed ByRef.
MsgBox("After PassByRef: MyRefType.RefTypeProp = " & referenceType.RefTypeProp)
End Sub
Public Sub PassByVal(ByVal refType As MyRefType)
Dim newRefType As New MyRefType()
newRefType.RefTypeProp = 1000
refType = newRefType
'-This assignment will NOT persist after method call because of ByVal.
MsgBox("Inside PassByVal: MyRefType.RefTypeProp = " & refType.RefTypeProp)
End Sub
Public Sub PassByRef(ByRef reftype As MyRefType)
Dim newRefType As New MyRefType()
newRefType.RefTypeProp = 2000
'-This assignment will persist after method call because of ByRef.
reftype = newRefType
MsgBox("Inside PassByRef: MyRefType.RefTypeProp = " & reftype.RefTypeProp)
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