VB.NET 中的参考资料

发布于 07-10 22:26 字数 1984 浏览 12 评论 0原文

我有点不清楚的是 VB.NET 中类的引用(指针?)。 我要问的问题可以通过一些测试来回答,但我想知道是否有人可以发布一个像样的解释(或链接)。

如果您创建一个类:

Public Class ReferenceClass

    Private myBooleanValue As Boolean = False
    Public Property BooleanValue As Boolean
        Get
            Return myBooleanValue
        End Get
        Set(value As Boolean)
            myBooleanValue = value
        End Set
    End Property

End Class

然后创建一个实际使用该类作为属性的类:

Public Class UsingClass

     Private myReference As ReferenceClass
     Public Property Reference As ReferenceClass
        Get
             return myReference
         End Get
         Set(value As ReferenceClass)
             myReference = value
         End Set
     End Property

     Public Sub New(ByVal Reference As ReferenceClass)
         myReference = Reference
     End Sub

End Class

然后像这样使用它:

Public Class RuntimeOrSomething

     Public Shared myReference As ReferenceClass
     Public Shared ReadOnly Property Reference As ReferenceClass
         Get
             If myReference Is Nothing Then myReference = new ReferenceClass()
             return myReference
         End Get
     End Property

     Public Shared Function BooleanCheck() As Boolean
         Reference.BooleanValue = True
         Dim tempClass As New UsingClass(Reference)
         tempClass.Reference.BooleanValue = False

         Return (tempClass.Reference.BooleanValue = Reference.BooleanValue)
     End Sub

     Public Shared Sub DoNothing()
          Reference.BooleanValue = True
          Dim someBoolean As Boolean = BooleanCheck

          ' Now Reference.Booleanvalue is "False"
     End Sub

End Class

现在函数 BooleanCheck 将始终返回 true,即使尽管引用是“按值”而不是按引用传递给新类 UsingClass 的。 因此,不会创建类的副本,但 UsingClass 中的局部变量 myReference 仍然引用/指向 中的属性 Reference RuntimeOrSomething

这该如何优雅地解释呢?

Somewhat unclear to me are references (pointers?) to classes in VB.NET. The question I am about to ask can be answered by a little bit of testing, but I was wondering if anybody could post a decent explanation (or links, too).

If you create a class:

Public Class ReferenceClass

    Private myBooleanValue As Boolean = False
    Public Property BooleanValue As Boolean
        Get
            Return myBooleanValue
        End Get
        Set(value As Boolean)
            myBooleanValue = value
        End Set
    End Property

End Class

And then a class which actually uses this class as a property:

Public Class UsingClass

     Private myReference As ReferenceClass
     Public Property Reference As ReferenceClass
        Get
             return myReference
         End Get
         Set(value As ReferenceClass)
             myReference = value
         End Set
     End Property

     Public Sub New(ByVal Reference As ReferenceClass)
         myReference = Reference
     End Sub

End Class

And then use it like this:

Public Class RuntimeOrSomething

     Public Shared myReference As ReferenceClass
     Public Shared ReadOnly Property Reference As ReferenceClass
         Get
             If myReference Is Nothing Then myReference = new ReferenceClass()
             return myReference
         End Get
     End Property

     Public Shared Function BooleanCheck() As Boolean
         Reference.BooleanValue = True
         Dim tempClass As New UsingClass(Reference)
         tempClass.Reference.BooleanValue = False

         Return (tempClass.Reference.BooleanValue = Reference.BooleanValue)
     End Sub

     Public Shared Sub DoNothing()
          Reference.BooleanValue = True
          Dim someBoolean As Boolean = BooleanCheck

          ' Now Reference.Booleanvalue is "False"
     End Sub

End Class

Now the function BooleanCheck will always return true, even though the reference is passed to the new class UsingClass "by value", not by reference. So a copy of the class is not made, but the local variable myReference in UsingClass still references/points to the property Reference in RuntimeOrSomething.

How can this be explained elegantly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流年已逝2024-07-17 22:26:42

引用指向对象的实例,但它不是对象的实例。 复制该对象的方向不会创建另一个对象,而是创建另一个也指向同一对象的引用。

A reference points to an instance of an object, it is not an instance of an object. Making a copy of the directions to the object does not create another object, it creates another reference that also points to the same object.

甜心小果奶2024-07-17 22:26:42

我现在不能花太多时间来回答这个问题——在火车上打字,膝盖上坐着一个小孩——但我有几篇文章可能会有所帮助。 它们是关于 C# 编写的,但同样适用于 VB.NET:

I can't put much time into this answer now - typing on a train with a toddler on my knee - but I have a couple of articles which might help. They're written about C#, but the same applies to VB.NET:

别想她2024-07-17 22:26:42

来自 MSDN

如果您通过以下方式传递变量参数
使用 ByVal 关键字的值,
过程无法修改变量
本身。 但是,如果参数是
引用类型,可以修改
它所指向的对象的成员
点,即使你无法替代
对象本身。
特别是,您
可以修改对象的成员。
例如,如果参数是
数组变量,您不能分配
新数组,但您可以更改
它的一个或多个元素。 这
更改的元素反映在
底层数组变量
调用代码。

由于您的 ReferenceClass 是引用类型,因此如果您通过 ByVal 传递它,则无法将其替换为新对象(您不会这样做),但您可以对其内部结构进行修改(您会这样做)。 无论您传递 ByRef 还是 ByVal,修改其内部结构仍然会“影响”原始对象(因为内存中只有一个对象)。

From MSDN:

If you pass a variable argument by
value using the ByVal keyword, the
procedure cannot modify the variable
itself. However, if the argument is a
reference type, you can modify the
members of the object to which it
points, even though you cannot replace
the object itself.
In particular, you
can modify the members of the object.
For example, if the argument is an
array variable, you cannot assign a
new array to it, but you can change
one or more of its elements. The
changed elements are reflected in the
underlying array variable in the
calling code.

Since your ReferenceClass is a reference type, if you pass it ByVal, you can't replace it with a new object (which you don't), but you can muck around with its innards (which you do). Whether you pass ByRef or ByVal, mucking around with its innards will still "affect" the original object (since there is only ever one object in memory).

鹿港小镇2024-07-17 22:26:42

在这一行中:

Dim tempClass as New UsingClass(Reference)

Reference 属性引用的对象是“按值”传递的,但它不是被复制的对象它是对复制的对象的引用(即 myReference 和 tempClass.Reference 是指向同一对象的两个不同的“指针”。然后您可以执行 tempClass.Reference = new ReferenceClass然后 myReference 和 tempClass.Reference 仍然是两个不同的“指针”,但现在它们各自指向两个不同的对象。

In this line:

Dim tempClass as New UsingClass(Reference)

the object referred to by the Reference property is passed "by value", but it's not the object that is copied, it's the reference to that object that is copied (i.e. myReference and tempClass.Reference are two distinct "pointers" to the same object. You could then do tempClass.Reference = new ReferenceClass and then myReference and tempClass.Reference are still two distinct "pointers", but now they each point to two different objects.

寂寞清仓2024-07-17 22:26:42

在 VB.NET 中通过 byval/byref 传递类时,可以将其视为 C 编程和指针,例如 -

ByVal = passing arguments via - a pointer 
ByRef = passing arguments via - a pointer to a pointer

以字符串为例

' ByRef - modify str pointer to "point" to a new string
Sub Test_Ref(ByRef str as string)
    str = "New String ByRef"
End Sub

' ByVal - can't modify str pointer must return a (pointer to) new string
Function Test_Val(ByVal str as String) as String
    Return "New String ByVal"
End Sub

Sub Main()
    Dim strTest as String = "Hello World!"
    Console.WriteLine(strTest)
    Test_Ref(strTest)
    Console.WriteLine(strTest)
    Test_Val(strTest)
    Console.WriteLine(strTest) ' oops still pointing to same string
    strTest = Test_Val(strTest)
    Console.WriteLine(strTest) ' that's better :)
End Sub

When passing classes byval/byref in VB.NET it is possible to think of it in terms of C programming and pointers such that -

ByVal = passing arguments via - a pointer 
ByRef = passing arguments via - a pointer to a pointer

Take strings as an example

' ByRef - modify str pointer to "point" to a new string
Sub Test_Ref(ByRef str as string)
    str = "New String ByRef"
End Sub

' ByVal - can't modify str pointer must return a (pointer to) new string
Function Test_Val(ByVal str as String) as String
    Return "New String ByVal"
End Sub

Sub Main()
    Dim strTest as String = "Hello World!"
    Console.WriteLine(strTest)
    Test_Ref(strTest)
    Console.WriteLine(strTest)
    Test_Val(strTest)
    Console.WriteLine(strTest) ' oops still pointing to same string
    strTest = Test_Val(strTest)
    Console.WriteLine(strTest) ' that's better :)
End Sub
溺孤伤于心2024-07-17 22:26:42

如上所述,将一个对象变量设置为等于另一个对象变量只是将“指针”设置为内存中同一对象的实例,如果您想克隆该对象,请

Public Function Clone() As Object Implements ICloneable.Clone
Return Me.MemberwiseClone()
End Function

在分配该对象时使用类似 With 之 类的方法实现 iCloneable 接口使用克隆方法

Dim tempClass as ReferenceClass = Reference.Clone

Like said above, setting a object variable equal to another just sets the "pointer" to an instance of the same object in memory, if you want to clone the object look at implementing the iCloneable interface with something like

Public Function Clone() As Object Implements ICloneable.Clone
Return Me.MemberwiseClone()
End Function

With and when you allocation the object use the clone method

Dim tempClass as ReferenceClass = Reference.Clone
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文