Public Property size()
Get
Return _size
End Get
Set(ByVal Value)
_size = Value
End Set
End Property
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a1 As A
Dim t As Int32
a1 = New A
a1.size = 8
t = a1.size
updateA1ByVal(a1)
If Not IsNothing(a1) Then
Console.WriteLine("a1 update by val from " & t & " to " & a1.size)
Else
Console.WriteLine("a1 changed to nothing by updateA1ByVal")
End If
updateA1ByRef(a1)
If Not IsNothing(a1) Then
Console.WriteLine("a1 update by ref from " & t & " to " & a1.size)
Else
Console.WriteLine("a1 changed to nothing by updateA1ByRef")
End If
End Sub
Private Sub updateA1ByVal(ByVal a As A)
a.size = 9
a = Nothing
End Sub
Private Function updateA1ByRef(ByRef a As A)
a.size = 9
a = Nothing
End Function
执行结果
a1 update by val from 8 to 9
a1 changed to nothing by updateA1ByRef
套用一句大师的话
In C++: pass object by value , pass object by reference 。
In Java: pass object reference by value
也许我们可以这样说
In vb.net: pass object reference by value, pass object by reference。