Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vb.net - ByVal and ByRef with reference type

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...