Why does the sender behave like it was passed as byref?

is not the sender passed as byval? If so, why does the sender object behave as if it were passed as byref. We were able to change the button text on the button sender, apparently in the code below.

Private Sub Button_Click(byval sender As Object,byval  e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
    DirectCast(sender, Button).Text = "You clicked me!"
End Sub

      

+3


source to share


2 answers


A copy of the reference to the sender is passed to the method. This means that when you change the text, you change it on the same object. I suggest you read this great article by Jon Skeet: Parameters



+3


source


ByVal sends a copy when you use a primitive, string, or structure (Boolean, integer, dateline, etc.). When you submit an object, it is not copied, but its reference is copied. Therefore, you are working with the same object. A button is an object.



+1


source







All Articles