无法设置属性的属性
正如我所说,我在设置财产的财产时遇到了麻烦。 假设我有一个代表事务的类。 在我的类中,我有一个代表另一个类的属性,例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道这是否是复制粘贴错误,但您似乎每次都在属性的“获取”部分中覆盖
_myPerson
。 这很可能会导致你的“Some Name”被覆盖:-)编辑do'h,codeape在我之前几秒说了同样的话:-)
常见模式对于像这样的属性的延迟初始化,可以执行如下操作,在覆盖对象之前检查对象是否为 null。
您必须将
_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.
You'd have to change your
_myPerson
initialisation to be a straightdim _myPerson as Person
, rather thandim _myPerson as New Person()
too每次执行 get
.MyPerson
时,都会调用函数Person.GetAppropriatePerson
。我不知道该函数的实现,但我猜测每次调用它时它都会返回一个新的 Person 对象。
您更改一个 Person 实例的名称。 下次调用
.MyPerson
时,将返回另一个 Person 实例。根据其工作方式,您可以执行一些操作,例如:
_myPerson
,并将MyPerson
属性设置为只读。_myPerson
初始化为null
,然后在MyPerson
getter 中添加if _myPerson == null then _myPerson = GetAppropriatePerson 等
代码>Every time you do a get
.MyPerson
, the functionPerson.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:
GetAppropriatePerson
in the constructor (if personid is known at that time). Assign the return value to_myPerson
, and make theMyPerson
property read-only._myPerson
tonull
, then in theMyPerson
getter have aif _myPerson == null Then _myPerson = GetAppropriatePerson etc.
它必须被这一行改变:
_myPerson = Person.GetAppropriatePerson(Me.PersonID)
单步执行您的代码...
It must be getting changed by this line:
_myPerson = Person.GetAppropriatePerson(Me.PersonID)
step through you code...