带引用类型的 ByVal 和 ByRef [英] ByVal and ByRef with reference type

查看:28
本文介绍了带引用类型的 ByVal 和 ByRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码:

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

form_load 中的消息框打印: Thursday ,表示对象(TypeTest)是通过引用传递的.对调用函数中的 t1 参数使用 ByVal 和 ByRef 有什么区别:Test.

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.

推荐答案

由于您将 TypeTest 声明为 Class,这使其成为引用类型(与用于声明值类型Structure相反).引用类型变量充当指向对象的指针,而值类型变量直接存储对象数据.

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.

您的理解是正确的,ByRef 允许您更改参数变量的值,而 ByVal 则不允许.使用值类型时,ByValByRef 之间的区别非常明显,但是当您使用引用类型时,行为有点不那么预期.您可以更改引用类型对象的属性值的原因,即使它是通过 ByVal 传递的,因为变量的值是指向对象的指针,不是对象本身.更改对象的属性根本不会更改变量的值.该变量仍然包含指向同一对象的指针.

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.

这可能会让您相信 ByValByRef 之间对于引用类型没有区别,但事实并非如此.它们是有区别的.不同之处在于,当您将引用类型参数传递给 ByRef 参数时,您调用的方法可以更改原始变量所指向的对象.换句话说,该方法不仅能够更改对象的属性,而且还能够将参数变量完全指向不同的对象.例如:

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

这篇关于带引用类型的 ByVal 和 ByRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆