Trey wrote:
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);
}
}
--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B