无法设置属性的属性

时间:2021-07-16 00:23:41

I am having trouble with, as I said, setting a property's property. Let's say I have a class that represents a transaction. In my class I have a property that represents another class, such as this:

正如我所说,我正在设置房产的财产。假设我有一个代表交易的类。在我的课堂上,我有一个代表另一个类的属性,例如:

Public Class PersonRecord

    _myPerson = new Person()

    Public Property MyPerson as Person
    Get
        _myPerson = Person.GetAppropriatePerson(Me.PersonID)

        return _myPerson
    End Get

    Set
        _myPerson = value
    End Set
    End Property

So I essentially have a property that has a get filter that gets the appropriate person. The problem is that when I want to set the Person's info through the property, VB seems to ignore that I even did it, such as this:

所以我基本上有一个属性,有一个get过滤器,可以获得适当的人。问题是,当我想通过属性设置Person的信息时,VB似乎忽略了我甚至做了它,例如:

Me.myPersonRecord.Person.Name = "Some Name"

But when I put a watch on this, after setting the property, my value does not change. I am puzzled by this behavior. Is there something I'm doing wrong? Thanks!

但是当我对此进行监视时,在设置属性后,我的值不会改变。我对这种行为感到困惑。有什么我做错了吗?谢谢!

3 个解决方案

#1


Every time you do a get .MyPerson, the function Person.GetAppropriatePerson gets called.

每次执行get.MyPerson时,都会调用Person.GetAppropriatePerson函数。

I do not know the implementation of that function, but I would guess that it returns a new Person object every time that it is called.

我不知道该函数的实现,但我猜它每次调用它都会返回一个新的Person对象。

You change the Name of one Person instance. The next time you call .MyPerson, another Person instance gets returned.

您更改一个Person实例的名称。下次调用.MyPerson时,会返回另一个Person实例。

Depending on how this is supposed to work, you could do a few things, for instance:

根据这应该如何工作,你可以做一些事情,例如:

  • Call GetAppropriatePerson in the constructor (if personid is known at that time). Assign the return value to _myPerson, and make the MyPerson property read-only.
  • 在构造函数中调用GetAppropriatePerson(如果当时知道personid)。将返回值分配给_myPerson,并将MyPerson属性设置为只读。

  • Or initialize _myPerson to null, then in the MyPerson getter have a if _myPerson == null Then _myPerson = GetAppropriatePerson etc.
  • 或者将_myPerson初始化为null,然后在MyPerson getter中有一个if _myPerson == null然后_myPerson = GetAppropriatePerson等。

#2


I don't know if it's a copy n paste error, but you appear to be overwriting _myPerson every time in the Get part of the property. That would more than likely cause your "Some Name" to get overwritten :-)

我不知道它是否是一个复制n粘贴错误,但你似乎每次都在属性的Get部分覆盖_myPerson。这很可能会导致你的“某些名字”被覆盖:-)

edit do'h, codeape says the same thing seconds before me :-)

编辑do'h,codeape在我面前说了同样的事情:-)

A common pattern for lazy initialisation of properties like this is to do something like the following, which checks if the object is null before overwriting it.

像这样延迟初始化属性的常见模式是执行类似下面的操作,在覆盖之前检查对象是否为null。

Public Property MyPerson as Person
Get
    If _myPerson Is Nothing Then
        _myPerson = Person.GetAppropriatePerson(Me.PersonID)
    End If
    return _myPerson
End Get

You'd have to change your _myPerson initialisation to be a straight dim _myPerson as Person, rather than dim _myPerson as New Person() too

你必须改变你的_myPerson初始化为一个直接昏暗的_myPerson作为Person,而不是将_myPerson作为New Person()变暗

#3


It must be getting changed by this line: _myPerson = Person.GetAppropriatePerson(Me.PersonID)

它必须被这一行改变:_myPerson = Person.GetAppropriatePerson(Me.PersonID)

step through you code...

通过你的代码...

#1


Every time you do a get .MyPerson, the function Person.GetAppropriatePerson gets called.

每次执行get.MyPerson时,都会调用Person.GetAppropriatePerson函数。

I do not know the implementation of that function, but I would guess that it returns a new Person object every time that it is called.

我不知道该函数的实现,但我猜它每次调用它都会返回一个新的Person对象。

You change the Name of one Person instance. The next time you call .MyPerson, another Person instance gets returned.

您更改一个Person实例的名称。下次调用.MyPerson时,会返回另一个Person实例。

Depending on how this is supposed to work, you could do a few things, for instance:

根据这应该如何工作,你可以做一些事情,例如:

  • Call GetAppropriatePerson in the constructor (if personid is known at that time). Assign the return value to _myPerson, and make the MyPerson property read-only.
  • 在构造函数中调用GetAppropriatePerson(如果当时知道personid)。将返回值分配给_myPerson,并将MyPerson属性设置为只读。

  • Or initialize _myPerson to null, then in the MyPerson getter have a if _myPerson == null Then _myPerson = GetAppropriatePerson etc.
  • 或者将_myPerson初始化为null,然后在MyPerson getter中有一个if _myPerson == null然后_myPerson = GetAppropriatePerson等。

#2


I don't know if it's a copy n paste error, but you appear to be overwriting _myPerson every time in the Get part of the property. That would more than likely cause your "Some Name" to get overwritten :-)

我不知道它是否是一个复制n粘贴错误,但你似乎每次都在属性的Get部分覆盖_myPerson。这很可能会导致你的“某些名字”被覆盖:-)

edit do'h, codeape says the same thing seconds before me :-)

编辑do'h,codeape在我面前说了同样的事情:-)

A common pattern for lazy initialisation of properties like this is to do something like the following, which checks if the object is null before overwriting it.

像这样延迟初始化属性的常见模式是执行类似下面的操作,在覆盖之前检查对象是否为null。

Public Property MyPerson as Person
Get
    If _myPerson Is Nothing Then
        _myPerson = Person.GetAppropriatePerson(Me.PersonID)
    End If
    return _myPerson
End Get

You'd have to change your _myPerson initialisation to be a straight dim _myPerson as Person, rather than dim _myPerson as New Person() too

你必须改变你的_myPerson初始化为一个直接昏暗的_myPerson作为Person,而不是将_myPerson作为New Person()变暗

#3


It must be getting changed by this line: _myPerson = Person.GetAppropriatePerson(Me.PersonID)

它必须被这一行改变:_myPerson = Person.GetAppropriatePerson(Me.PersonID)

step through you code...

通过你的代码...