ByVal vs. ByRef (and also Value Type vs. Reference Type)
There are different behaviours of passing a parameter using ByVal and ByRef between Value Type and Reference Type.
For Value Type (most primitives and Structure objects):
- ByVal copies the value of the parameter. Changes will not affect original object.
- ByRef uses the value of the parameter. Changes will affect original object.
For Reference Type (Class objects):
- ByVal copies the pointer to the value of the parameter. Changes will affect the original object. Unless you changed the reference of the object (e.g.: assigning a different object to the parameter, setting the parameter to Nothing)
- ByRef uses the pointer to the value of the parameter. Changes will affect the original object, including reference changes.
Now, things get interesting when you're assigning a local variable to the referenced object (a.k.a. doing operation with the parameter outside the original method call). It seems that when you do that, the local variable copied the reference to the referenced object (just like ByVal). So when you modify the value of local variable, the changes will affect the original object, but reference changes will not.
Is it any way possible to keep references even outside the method call?