在VB.Net参考 [英] References in VB.Net

查看:234
本文介绍了在VB.Net参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点不清楚我是引用(指针?),以班级在VB.Net。我要问的问题可以通过测试的一点点来回答,但我想知道如果任何人都可以发布一个体面的解释(或链接,太)。

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).

如果你创建一个类:

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

现在的功能 BooleanCheck 总是返回,即使引用传递给新类 UsingClass 按价值,而不是参考。因此,类的副本不言,但 myReference UsingClass 的局部变量仍然引用/指向性参考 RuntimeOrSomething

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? Any feedback and comments about this topic are welcome.

推荐答案

一个参考点到对象的一个​​实例,它不是一个对象的一个​​实例。制作的方向副本的对象不创建另一个对象,它创建另一个引用也指向同一个对象。

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.

这篇关于在VB.Net参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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