C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ByVal: 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.
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 subroutine after Example2 returns.
VB.NET program that shows ByVal and ByRef
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
10