Accessing parent with nested class?

  • Thread starter Thread starter Trey
  • Start date Start date
T

Trey

Is it possible to access the parent from an instance of a "child"
class? For example in the code below I would like to be able to
reference which exact army a troop belonged to by doing something like
myTroop.Parent().

If not, is there a home grown solution I can implement short of
creating an army "id" and dealing with key management between troop <->
army? I'm basically looking for cheap relationship management.
Thanks,
Trey

internal class Army
{
int myMember;
internal Troop[] Troops = new Troop[100];

Army()
{
}

internal class Troop
{
Troop()
{
}
}
}
 
Is it possible to access the parent from an instance of a "child"

There is nothing supplied in the language to do this (Java does have the
feature you are looking for, you would just say Army.this from inside the
nested type).

I think the common workaround here is to have a reference to an Army stored
inside each Troop. Perhaps someone else knows of a more clever solution.
 
Trey said:
Is it possible to access the parent from an instance of a "child"
class? For example in the code below I would like to be able to
reference which exact army a troop belonged to by doing something like
myTroop.Parent().
<snip>

You're confusing declaration scope with instance scope.

Just because the internal class Troop is declared inside the class Army,
the object instances are completely separate.

You could argue that the effect is similar to this:

internal class Army
{ ... }

namespace Army
{
internal class Troop
{ ... }
}

of course, this is invalid syntax, but it is somewhat similar in nature.
The fulls cope name for Troop is namespace.Army.Troop, but it's a
separate class from Army.

This means that the only way for Troop to access any Army object, it
needs a reference to a live Army object to do so.

The typical solution is to introduce a member that contains a reference
to the outer class, somewhat like this:

internal class Army
{
public Army()
{
Troop t = new Troop(this);
...
}

internal class Troop
{
private Army _Army;
public Troop(Army army)
{
_Army = army;
}
public void GoAWOL()
{
_Army.Combatants.Remove(this);
}
}
 
Thanks for the replies. I currently pass in the "army id" to the Troop
constructor and use that for looking back up the hierarchy. I use an
integer for "army id". Would there be any significant perf hit if I
use what Lasse suggested and pass in the entire Army object (ByVal is
default, right?) to the child troop? I'm anticipating perf issues
later on so I'm trying to be as conservative as possible.

I'm somewhat splitting the thread here but I'm curious what advantage a
nested class provides? Simply that a child (troop) can only exist w/in
the context of a parent (army)? Anything else?
Thanks,
Trey
 

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

Back
Top