Dumb question: How do I get an object's parent?

  • Thread starter El Guerrero del Interfaz
  • Start date
E

El Guerrero del Interfaz

Hi,


I know it's a dumb question but then I'm newbie, so sorry about
that.

class MyObject {
anotherClass innerObject;
}
..
..
..
MyObject myObject = new MyObject();
..
..
..

What I want to do is how, knowing innerObject, do I get a reference
to the myObject instance that contains it? Is it possible?


Bye and thanks.
 
M

Matt Humphrey

El Guerrero del Interfaz said:
Hi,


I know it's a dumb question but then I'm newbie, so sorry about
that.

class MyObject {
anotherClass innerObject;
}
.
.
.
MyObject myObject = new MyObject();
.
.
.

What I want to do is how, knowing innerObject, do I get a reference
to the myObject instance that contains it? Is it possible?

You put a reference to myObject inside of innerObject. The problem is that
there's no such thing as "parent" object. innerObject could be referenced
by any number (well, 1 or more) other objects, so if you want to go from
innerObject to myObject you must store that association somewhere (in
innerObject, in a separate object or index, by search, etc).

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
S

Steve Horsley

El said:
Hi,


I know it's a dumb question but then I'm newbie, so sorry about
that.

class MyObject {
anotherClass innerObject;
}
.
.
.
MyObject myObject = new MyObject();
.
.
.

I believe that code running in methods of the inner object can
get at the outer instance by referring to "MyObject.this". e.g.

mummy = MyObject.this;

However, having a reference to the inner object doesn't help
unless a method of the inner object contains this code. You
really need the inner object's help in this respect.

I would query the need to find the outer object unless the inner
object is written to explicitly give you that information.

Steve
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,138
Latest member
NevilleLam
Top