
What is the difference between parameters that are received ByVal, and parameters received ByRef in the VB.NET programming language? To answer this question, we test ByVal and ByRef and see how their behavior differs.
This VB article covers the ByVal and ByRef keywords. These keywords describe formal parameters in Functions.

This program introduces two subs other than the Main subroutine: the Example1 method, which receives an integer parameter ByVal, and the Example2 method, which receives an integer ByRef. The Main() method then demonstrates the difference with a local variable of Integer type.
Program that demonstrates ByVal and ByRef [VB.NET]
Module Module1
Sub Main()
Dim value As Integer = 1
' The integer value doesn't change here when passed ByVal.
Example1(value)
Console.WriteLine(value)
' The integer value DOES change when passed ByRef.
Example2(value)
Console.WriteLine(value)
End Sub
Sub Example1(ByVal test As Integer)
test = 10
End Sub
Sub Example2(ByRef test As Integer)
test = 10
End Sub
End Module
Output
1
10Effect of ByVal (Example1). When the integer value is passed to the Example1 sub, its value is only changed inside the Example1 subroutine. When control returns to Main(), the value is unchanged from before Example1 was invoked. This is because ByVal passes a copy of the bytes of the variable.
Effect of ByRef (Example2). Next, the integer value is passed to the Example2 subroutine, which receives it ByRef. In Example2, the reference to the integer is copied, so when the value is changed, it is reflected in the Main sub. Finally, the value is changed to 10 in the Main method after Example2 returns.

The program here used Integers, which are a value type. With object references, you are dealing with a value that indicates a memory location. So if you pass an object ByVal, you are copying the bytes in that reference. If you access or mutate fields or methods on that copied reference, the changes will be reflected everywhere in the program. But if you reassign the reference itself, it will not be reflected in the calling location.

Understanding the difference between ByVal and ByRef is important in VB.NET programming. ByVal is often useful for references and also values; ByRef is typically more useful for values because you more often need to change the original values.
VB.NET Tutorials