"Dennis" <De****@discussions.microsoft.com> schrieb
Then when a reference object is passed byVal, you can still change
it in the function that you called...so what's the difference
between passing a reference object byVal or byRef...you can change
the object no matter how you pass it!
Right, but if you pass it ByRef, you have a reference to the original
reference and you can modify the original reference. If you pass it ByVal,
the original reference can not be modified.
Example:
Shared Sub main()
Dim s As String = "before"
RefTypeByVal(s)
Debug.WriteLine(s) 'still "before"
RefTypeByRef(s)
Debug.WriteLine(s) 'now "new string"
End Sub
Shared Sub RefTypeByVal(ByVal s As String)
s = "new string"
End Sub
Shared Sub RefTypeByRef(ByRef s As String)
s = "new string"
End Sub
Actually it is very simple:
ByVal: A copy of the value is passed.
ByRef: A reference to the value is passed.
The value can either be an object or a reference to an object.
That's all you have to remember.
Armin