Setting obj2.property = obj1

If I have a class, like the one below:

    Class Class1
    {
        public Class1 Parent = null;

        public Class1()
        {
        }
    }

and in my program, I do:

    public static Main()
    {
        Class1 obj1 = new Class1();
        Class1 obj2 = new Class1();

        obj2.Parent = obj1;
    }

does obj2.Parent contain a pointer to obj1 or is it a copy?  If I wanted a pointer, would I use

        obj2.Parent = ref obj1;

J.
LVL 16
jimbobmcgeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Parent is an object variable, which by it's very nature is a reference type.  Reference types are basically pointers to a memory location that contains the actual object.  

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aseem_dayalCommented:
obj2.Parent will be a reference/pointer to obj1.

Incase you need a copy on obj1 to refered by obj2.Parent, I suggest that you implement the ICloneable interface in Class1. Let me know if you need any sample code on this.

Aseem
jimbobmcgeeAuthor Commented:
Thanks.  So doing:

    obj2.Parent = obj1;
    obj1 = null;

would make obj2.Parent null too.  OK.  Taking a slightly different approach, the following:

    public void Main()
    {
        Hashtable ht = new Hashtable;
        Class1 obj1 = new Class1();
        Class1 obj2 = new Class1();

        obj2.Parent = obj1;

        ht.Add("Object1", obj1);
        ht.Add("Object2", obj2);

        obj1 = null;
        obj2 = null;
    }

would give me a copy of obj1 and obj2 in ht, but ht["Object2"].Parent (sic) would be null.  And if I did:

        ht["Object2"].Parent = ht["Object1"]     //(sic)

then ht["Object2"].Parent would be a pointer to ht["Object1"].

Am I correct so far?

J.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Bob LearnedCommented:
What is the requirement driving this?

Bob
"Bigger Picture Guy"
jimbobmcgeeAuthor Commented:
Just learning, really.  I want to create a simple game engine, wrapping Managed DirectX, but I'm taking it one small step at a time.

Ultimately I want to have a enumerable collection of objects but I won't know how many until I load them in from a level/map file.  When I get to my drawing section am toying with the logic that:

    <PSEUDO>
        for each gameobject in engine.gameobjectcollection
            if gameobject.parent = null then gameobject.draw()
            elseif gameobject.parent.isdrawn and gameobject.parent.drawchildren then gameobject.draw
        next gameobject
    </PSEUDO>

Since I'll be loading these from a file, I figured that I would try:

    <PSEUDO>
        for each entry in file
            obj = new gameobject
            obj.name = entry.attributes.name
            obj.parent = entry.attributes.parent
            obj.position = entry.attributes.position    // etc
            obj.register()   // i.e. engine.gameobjectcollection.add(this.name, this)
            obj = null
        next entry
    </PSEUDO>

Obviously, when the loading loop is finished, the individual objects will lose scope, but the collection will not.  My question concerns whether I can get a suitable reference to the parent object, in this manner.  I don't wish to make changes to a local object, if its not going to be propegated to the collection.

It all comes down to whether I should say that each gameobject references a parent object, or each gameobject contains a collection of child gameobjects.  I figured that recursive enumeration of child objects may be more resource intensive than a linear enumeration through a primary collection, so I'm investigating the Parent option first.

As a basic example:  Consider a room in a map -- that is derived from a gameobject.  It contains a character model -- that is derived from a gameobject.  It's made up of bones -- these are also derived from gameobjects.  The room's draw method could be:

    public override void Draw()
    {
        if (this.Parent is null || (this.Parent.IsDrawn && this.Parent.DrawChildren))
        {
            this.DrawGeometry();    // Some private function to draw the room geometry
            this.IsDrawn = true;
            this.DrawChildren = true;
        }
    }

whereas character's draw method could simple be:

    public override void Draw()
    {
        if (this.Parent is null || (this.Parent.IsDrawn && this.Parent.DrawChildren))
        {
            // Do this if containing room is drawn
            // No implicit drawing
            this.IsDrawn = true;
            this.DrawChildren = true;
        }
    }

whereas the bone's draw method:

    public override void Draw()
    {
        if (this.Parent is null || (this.Parent.IsDrawn && this.Parent.DrawChildren))
        {
            this.DrawMesh();    // Some private function to draw the actual model
            this.IsDrawn = true;
            this.DrawChildren = true;    // For extending, e.g. if arm.isdrawn then hand.draw()
        }
    }

In the main process, I loop through the collection for all gameobjects, invoking their draw routine.  I don't expect this to work first time and am looking to explore these ideas so I can better understand other people's ideas on the subject.

I know I've gone off-topic a bit but that's the bigger picture...

J.
jimbobmcgeeAuthor Commented:
It seems I have scared you all away...

J.
jimbobmcgeeAuthor Commented:
OK, I tested this and found that:

    obj2.Parent = obj1;  obj1 = null;

would set obj2.Parent = null, whereas:

    obj2.Parent = (Class1)ht["Object1"];

gives me the result that I am looking for.

So I'm all set...

Thanks,

J.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.