无法设置属性的属性

发布于 2024-07-25 08:15:34 字数 586 浏览 3 评论 0原文

正如我所说,我在设置财产的财产时遇到了麻烦。 假设我有一个代表事务的类。 在我的类中,我有一个代表另一个类的属性,例如:

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

因此,我本质上有一个属性,该属性具有一个获取适当人员的获取过滤器。 问题是,当我想通过属性设置Person的信息时,VB似乎忽略了我所做的事情,例如:

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

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

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:

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!

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

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

发布评论

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

评论(3

誰ツ都不明白 2024-08-01 08:15:34

我不知道这是否是复制粘贴错误,但您似乎每次都在属性的“获取”部分中覆盖 _myPerson 。 这很可能会导致你的“Some Name”被覆盖:-)

编辑do'h,codeape在我之前几秒说了同样的话:-)

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

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

您必须将 _myPerson 初始化更改为直接 dim _myPerson as Person,而不是 dim _myPerson as New Person()

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

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

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.

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

闻呓 2024-08-01 08:15:34

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

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

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

根据其工作方式,您可以执行一些操作,例如:

  • 在构造函数中调用 GetAppropriatePerson(如果当时已知 personid)。 将返回值分配给 _myPerson,并将 MyPerson 属性设置为只读。
  • 或者将 _myPerson 初始化为 null,然后在 MyPerson getter 中添加 if _myPerson == null then _myPerson = GetAppropriatePerson 等代码>

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

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.

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

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.
  • Or initialize _myPerson to null, then in the MyPerson getter have a if _myPerson == null Then _myPerson = GetAppropriatePerson etc.
倚栏听风 2024-08-01 08:15:34

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

单步执行您的代码...

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

step through you code...

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