Is it possible for a child (or child collection) to read the values of it's parent's properties directly? Let's say for example the following set of code is in the child object:
If parentObject.PropertyValue = "someValue" Then...
Is something like this possible?
Thanks,
jender624
A child should know as little about the parent as possible. You want to avoid tight coupling between the child and parent. To avoid this, create an interface that contains the information needed by the child. Implement this interface in the parent and pass the reference to the child. This will give you more flexibility and will allow any class that implements the interface to use that child.
I've thought and rethought this a few times, and here's my conclusion as of now.
In some cases, the child objects really only exist in the context of a specific parent container, and in that case I think it's ok. For example, an OrderLineItem may do some calculation based on some value from its parent Order object because it can always count on the Order being the parent container and it's always created by a method that passes a parentOrder parameter.
But as a general rule, an object should not know anything outside its little encapsulated world.
Sounds like it's not advised, in general. That's okay; I'll find another way of doing what I'm trying to do. Thanks for replying!
Copyright (c) Marimer LLC