473,599 Members | 3,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing parent with nested class?

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()
{
}
}
}

Nov 16 '05 #1
5 18576
> 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.
Nov 16 '05 #2
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" <tr*****@gmail. com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
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()
{
}
}
}

Nov 16 '05 #3
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.Combatant s.Remove(this);
}
}

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vk arlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #4
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

Nov 16 '05 #5
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" <tr*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9705
by: Heiko Henkelmann | last post by:
Please see the following example. Is there any other way to access SomeAttribute from the nested class, without having to use the actual name of the outer class? class SomeClass: SomeAttribute = 1 class SomeNestedClass: SomeOtherAttribute = 2
4
4417
by: Vedanta Barooah | last post by:
greetings.... in a python nested class is it possible to change the value of the parent class's variable without actually creating an instance of the parent class, consider this code: class mother: x=0 def __init__(self): self.x=1
4
4010
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best Regards KInd --
7
2655
by: newbiecpp | last post by:
Why this code cannot be compiled (under VC++ 7.0): class Outer { public: class Inner { public: Inner() : in_data(0) {} void action() { Outer::out_data++; // illegal reference to non-static
9
3679
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here? End Sub
6
3102
by: Marc Hoeijmans | last post by:
How is it possible to us a property from a nested class. Example: namespace MyApp { public class MaxLength { public class Project {
1
2570
by: Craig Buchanan | last post by:
is there an implicitly-defined Parent property defined for a class that is nested in another class? i'm looking to simplify my coding. At this point, i've defined the class as so: Class Item Private _SubItem As New SubItem(Me) 'other properties and methods
4
1678
by: =?Utf-8?B?YmlsbCB0aWU=?= | last post by:
I'm perusing a class that seems to utilize a nested class for private fields. In a nutshell, it looks as follows: public class Foo { public Property1 { get/set Bar.field1 }
3
3895
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
7992
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8051
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.