Please see the code below:
Public Class TypeTest
Public variable1 As String
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim t1 As TypeTest = New TypeTest
Test(t1)
MsgBox(t1.variable1)
End Sub
Public Sub Test(ByVal t1 As TypeTest)
t1.Variable1 = "Thursday"
End Sub
End Class
The message box in the form_load prints: Thursday, which means that the object (TypeTest) is passed by reference. What is the difference between using ByVal and ByRef for the t1 arguement in the function called: Test.
Since you declared TypeTest
as a Class
, that makes it a reference type (as opposed to Structure
which is used to declare value types). Reference-type variables act as pointers to objects whereas value-type variables store the object data directly.
You are correct in your understanding that ByRef
allows you to change the value of the argument variable whereas ByVal
does not. When using value-types, the difference between ByVal
and ByRef
is very clear, but when you're using using reference-types, the behavior is a little less expected. The reason that you can change the property values of a reference-type object, even when it's passed ByVal
, is because the value of the variable is the pointer to the object, not the the object itself. Changing a property of the object isn't changing the value of the variable at all. The variable still contains the pointer to the same object.
That might lead you to believe that there is no difference between ByVal
and ByRef
for reference-types, but that's not true. There is a difference. The difference is, when you pass a reference-type argument to a ByRef
parameter, the method that you're calling is allowed to change the object to which the original variable is pointing. In other words, not only is the method able to change the properties of the object, but it's also able to point the argument variable to a different object altogether. For instance:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim t1 As TypeTest = New TypeTest
t1.Variable1 = "Thursday"
TestByVal(t1)
MsgBox(t1.variable1) ' Displays "Thursday"
TestByRef(t1)
MsgBox(t1.variable1) ' Displays "Friday"
End Sub
Public Sub TestByVal(ByVal t1 As TypeTest)
t1 = New TypeTest()
t1.Variable1 = "Friday"
End Sub
Public Sub TestByRef(ByRef t1 As TypeTest)
t1 = New TypeTest()
t1.Variable1 = "Friday"
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With