Reference parent object of child object?

G

Guest

Is there a way to reference the "parent" object that creates a child object
from within the child object? Or do I have to pass a reference to the parent
object to the constructor of the child object so that the child object
"knows" who is creating it?

ie: how would I do something like this:

Object1 parent = new Object1();
parent.CreateObject2() // object1 creates a new object called object2
object2.parent // I want to be able to refer back to the
parent from the newly created "child object"

or when I create object2 do I have to pass a reference to "parent" in order
to reference it from within object2.

Thanks,

Alex
 
J

John Saunders

Alex said:
Is there a way to reference the "parent" object that creates a child
object
from within the child object? Or do I have to pass a reference to the
parent
object to the constructor of the child object so that the child object
"knows" who is creating it?

Yes. That's how you have to do it.


John Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top