Click here to Skip to main content
15,846,342 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a class that is structured like this:
class MyParent
    MyChild[] children = new MyChild[1];
    class MyChild
        //Ignore this, just for a demonstration
        int property1 = 5;
        string property2 = "Hello, this just shows why i might need to do this";
        public MyChild()
            /*reference to parent object*/.children[0] = this;

and I want to use it like this
MyParent parent = new MyParent();
parent.MyChild child = new MyChild();

So, I need to get a reference of the parent object from an instance of a child object. Is there any way to do this?

What I have tried:

I cannot find any way to do this, I've been looking forever. I DO NOT want to have to pass the parent object to it explicitly when declaring the child object, like
parent.MyChild child = new MyChild(parent);
Updated 9-Jun-23 0:00am
PIEBALDconsult 7-Jun-23 11:22am    
The only way is for the child to be told who its parent is.
The best way is for the child's constructor to take the parent as a parameter and if you don't want to do that, then maybe you can use a property and set it after instantiating the child.

Also, have you considered something along the lines of child = parent.CreateChild() so the parent can set the child's Parent property?

You're confusing "parents" and "name spaces".

It's not "parent.MyChild"; it's [name space].MyParent.MyChild ... and it's a (class) "type" you're trying to define (in this case).

The "parent" of "child" is the "MyChild[] children" array; which in turn is a "child" of an instance of MyParent (e.g. parent).

(Things would be simpler if you used a "generic list" instead of an array; e.g. List<mychild>. Then add a method to MyParent that is used to add new children (to that list). You still need a parent reference somewhere along the line: chicken and egg).
Share this answer
OK, lets go back a stage or two.

OOPs principles are simple: abstraction, inheritance, encapsulation, and polymorphism - and one of the things they imply is that two classes should not know details about each other because that means that they are locked together: you cannot change the internal workings of a class without knowing how the other class would be affected by the changes.

And that's bad. Very bad! It reduces reusability, and makes maintenance a nightmare.

What you are trying to do locks the parent and child together: the child can only be instantiated by an instance of the parent. And while that is simple to do (just pass the parent to the child constructor) it makes your code less reliable and a whole lot messier.

Instead, the child provides a property or method so that the parent can pass the data the child needs into it, and the parent handles an event the child raises to say "I need data".

Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] - the code won't be identical in your case because the child is requesting data, but it'll be very similar. And the parent passes back the data very easily: Transferring information between two forms, Part 1: Parent to Child[^]
Share this answer
Shahin Khorshidnia 7-Jun-23 13:17pm    
BillWoodruff 8-Jun-23 5:16am    
Yes ! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900