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!
Every time you do a get
.MyPerson, the functionPerson.GetAppropriatePersongets 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:
GetAppropriatePersonin the constructor (if personid is known at that time). Assign the return value to_myPerson, and make theMyPersonproperty read-only._myPersontonull, then in theMyPersongetter have aif _myPerson == null Then _myPerson = GetAppropriatePerson etc.