views:

327

answers:

2

Howdy,
Is it an acceptable strategy for a child object in a typical parent-child OO relationship to raise an event that the parent catches then passes a value back to the child through the EventArgs?

I have a situation where the child needs data from the parent, but the parent does not have that data at the time the child is instantiated so I can not pass it though a constructor.

Thank you,
Keith

A: 

Seems a little long-winded, unless I'm missing something. Why wouldn't the child just ask the parent for what it wants to know, and let the parent deal with whether or not something needs to be retrieved?

Mike Woodhouse
For the child to ask the parent that question, it would need a reference to the parent. That would require you instantiate the child with a reference to the parent.
Keith Sirmons
A: 

Why should the child raise the event, when it has no way of knowing if the parent already has that data?

If the parent has the child reference, why not let the parent feed the data to the child as soon as it has the data? No event needed, the parent could also just call a child method.

Passing in data in event args is ok, that is what they are for.

Tomalak