"If VB.Net manipulates objects by reference, will a swap work?" - This issue must certainly have troubled quiet a lot of beginners. VB.Net does manipulate objects by reference, and all object variables are references. On the other hand, VB.Net does not pass method arguments by reference; it passes them by value, (even if the method arguments are of reference type). Thus, a regular swap method will not work!
Let's come from the beginning, we know that there are four kinds of formal parameters in a VB.Net method declaration, namely
- Value parameters, which are declared without any modifiers. (default).
- Reference parameters, which are declared with the ref modifier.
- Output parameters, which are declared with the out modifier.
- Parameter arrays, which are declared with the params modifier.
Thus if there is no any method parameter keyword (ref or out), the parameter can have a value associated with it. That value can be changed in the method, but that value will not be reflected when the control passes back to the calling procedure. This is true for all value types. Now, consider a reference type, an object. If we pass an object to a method and if the value of its member is changed, it will be retained even when the control is passed back to the calling procedure! This is because objects are manipulated by reference.
If so, then, if we pass two objects to a regular swap method and come back to the calling procedure to see whether they have got swapped, they would have not! Look down the code below,
'swapping using ref
'swap.cs
Imports System
Namespace swap
Friend Class class1
Public x As Integer
Public y As Integer
Public Sub New(ByVal xval As Integer, ByVal yval As Integer)
x=xval
y=yval
End Sub
End Class
Friend Class SwapTest
Private Shared Sub swap_ref(ByRef param1 As class1, ByRef param2 As class1)
Dim temp As class1 = param1
param1 = param2
param2 = temp
End Sub
Private Shared Sub badswap_noref(ByVal param1 As class1, ByVal param2 As class1)
param1.x = 100 'even though the references are passed by value,
param1.y = 200 'we could very well assign or alter the values of the parameters
Dim temp As class1 = param1 'but swap won't work
param1 = param2
param2 = temp 'only the method references are swapped not the original ones
End Sub
Public Shared Sub Main(ByVal args As String())
'instantiating
Dim obj1 As class1 = New class1(0,0)
Dim obj2 As class1 = New class1(0,0)
'initial values of the instances obj1 and obj2
Console.WriteLine(" ")
Console.WriteLine("X: " & obj1.x & " Y: " & obj1.y)
Console.WriteLine("X: " & obj2.x & " Y: " & obj2.y)
Console.WriteLine(" ")
'calling badswap
badswap_noref(obj1,obj2) 'the two object references are passed by value to 'badswap_noref' method
Console.WriteLine("after badswap_noref")
Console.WriteLine("X: " & obj1.x & " Y:" & obj1.y)
Console.WriteLine("X: " & obj2.x & " Y:" & obj2.y)
Console.WriteLine(" ")
'calling swap_ref
Console.WriteLine("after swap_ref")
swap_ref(obj1,obj2) 'passing the objects by reference explicitly
Console.WriteLine("X: " & obj1.x & " Y:" & obj1.y)
Console.WriteLine("X: " & obj2.x & " Y:" & obj2.y)
Console.WriteLine(" ")
End Sub
End Class
End Namespace
Output of the above program is as follows.

Fig 1. Output of swap.vb.
In the above program, we pass the instances obj1 and obj2 of class1 to badswap_noref() method and see that they are not actually getting swapped. This is what actually happens in the badswap_noref method. Not the actual references obj1 and obj2 are passed. But a copy of the references is passed. So param1 and param2 contains a copy of the actual references obj1 and obj2 respectively. We are able to modify the values of X and Y in the badswap_noref method thru param1 and param2, because param1 and param2 still points to the same address locations referred to by obj1 and obj2. But what we are swapping is only param1 and param2, the method references, which are not retained when we come back to the calling procedure, Main().

Fig 2. Object reference and method reference.

Fig 3. Before and after badswap_noref.
Thus, in VB.Net, a swap method can be made possible by using the ref keyword. In such a case, we pass the object references explicitly by reference and not by value, thus able to swap them right in the called method.

Fig 4. Before and after swap_ref.
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).