Detail When the integer value is passed to Example1, its value is only changed inside the Example1 subroutine. In Main the value is unchanged.
Note ByVal passes a copy of the bytes of the variable (the value of it). It does not copy a storage location.
Detail 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 subroutine after Example2 returns.
Dim value As Integer = 1
' The integer value doesn't change here when passed ByVal.Example1(value)
' The integer value DOES change when passed ByRef.Example2(value)
Sub Example1(ByVal test As Integer)
test = 10
Sub Example2(ByRef test As Integer)
test = 10
Objects. 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—not the actual data pointed to by the reference.
Notes, continued. If you access or mutate fields or methods on a 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.
A summary. 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.