[Tutor] list of references to object properties
Alan Gauld
alan.gauld at btinternet.com
Sat Jan 19 01:04:42 CET 2013
On 18/01/13 11:11, Jose Amoreira wrote:
> Suppose I have a list l_obj of similar objects. Is there any way I can
> generate a list l_prp of references to a given property of those objects
> in such a way that, if change the value of one element in l_prp, the
> corresponding object in l_obj gets its property updated, and vice-versa?
Not easily, and for very good reason, it breaks one of the fundamental
principles of OOP. Objects should manage their own data and clients
should manage the objects.
So getting a list of the prp values is fine. Trying to modify those
values without going through the object is asking for trouble and
creating a nightmare for debugging. So unless you have a very specific
reason to do so just modify the objects prp through the object.
> As expected, changes in l_prp do not change the properties of the
> elements in l_obj, neither do changes in l_obj's element's properties
> change the values in l_prp.
As you say, it's as expected and very worrying if it did otherwise!
> Is there a simple way to implement such connections?
It's difficult by intent. It's to save you from yourself.
Is there a good reason to want to do such a thing?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list