PC Review


Reply
Thread Tools Rate Thread

Accessing parent with nested class?

 
 
Trey
Guest
Posts: n/a
 
      3rd Jan 2005
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()
{
}
}
}

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFya1Q=?=
Guest
Posts: n/a
 
      3rd Jan 2005
> Is it possible to access the parent from an instance of a "child"
> class?


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.
 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      3rd Jan 2005
Can you create a constructor on troop that looks like

private Army myArmy ;
Troop(Army parentArmy)
{
myArmy = parentArmy ;
}

and then wrap myArmy in a get Accessor property?

public Army WhoseUrArmy
{
get
{
return myArmy ;
}
}

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


"Trey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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()
> {
> }
> }
> }
>



 
Reply With Quote
 
=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=
Guest
Posts: n/a
 
      3rd Jan 2005
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/
private.php?do=newpm&u=
PGP KeyID: 0x0270466B
 
Reply With Quote
 
Trey
Guest
Posts: n/a
 
      3rd Jan 2005
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

 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      3rd Jan 2005
There won't be much perf. hit by passing in the entire object as ByVal as
the entire object is not copied, only a reference is copied - which is no
bigdeal.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik



"Trey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vb.net nested class' parent? Craig Buchanan Microsoft VB .NET 1 10th May 2006 07:59 PM
Nested class: accessing containing class's variables Joel Moore Microsoft VB .NET 9 31st Aug 2004 02:26 PM
Re: Accessing Nested IFrame's Parent ID and Names bruce barker Microsoft ASP .NET 1 14th Jul 2004 01:42 PM
Re: Accessing Nested IFrame's Parent ID and Names Marina Microsoft ASP .NET 1 13th Jul 2004 07:38 AM
Re: Accessing Nested IFrame's Parent ID and Names Curt_C [MVP] Microsoft ASP .NET 0 12th Jul 2004 04:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:07 PM.