473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How does a nested class reference it's containing class.

I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #1
12 4005
base is what you want:

int intBigClassID = base.ID_BigClass;

Mortos wrote:

I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #2
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClass;

{Snip}
--
I AM MORTOS!!!! (Switch spammer with blueyonder)
Nov 15 '05 #3

There's no automatic relation between a BigClass and a MiniClass
instance. You have to pass in a reference to the BigClass if you need
one.

public MiniClass{
public void MiniFunc(BigClass big){
int intBigClassID = big.ID_BigClass;
}
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4
Mortos wrote:
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClass;

{Snip}


There is no relationship between a nested type and its "container",
other than scoping. The creating of an instance of the nested type does
not imply any relationship at all to any particular instance of the
"containing" type. In this instance, "containing" applies _only_ to the
source code, not to any actual usage of the types.

If the nested type needs a reference to the type it was defined inside
of, then the outer type will need to pass a reference to itself to the
nested type's constructor and/or a property on the nested type.
Nov 15 '05 #5
Hi Mortos,

There is not a hardwired relationship between both classes, you can create
either one independently from the other like this:

BigClass.MiniClass instance = new BigClass.MiniClass()

BigClass instance = new BigClass()
Creating a BigClass instance does not create a MiniClass. I think that there
are two big differences between declaring both class at the same level or
one inside other:
1- Naming polution control, you decrease the number of types on the
namespace, as you will reference the inner class as ExternalClass.InnerClass
.. This also give an idea that the InnerClass is related to the OuterClass.

2- The inner class has access to the private members of the outer class, so
you can do this :
public class BigClass
{
private int ID_BigClass = -99;

public class MiniClass
{
public void MiniFunc(BigClass b)
{
int intBigClassID = b.ID_BigClass;
}
}

}

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mortos" <th*****@spammer.co.uk> wrote in message
news:_h*****************@news-binary.blueyonder.co.uk...
I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #6
base won't work, you are right Mortos. But how could this work with
non-statics anyway -- which outer class instance are you referring to?

Interestingly, the MS compiler was smart enough to tell what I was trying to
do:

C:\projects\testing\wintest1\WinTest1.cs(135): Cannot access a nonstatic
member of outer type 'wintest1.frmWinTest1.Outer' via nested type
'wintest1.frmWinTest1.Outer.Inner'

Indeed, making it static works:

public class Outer
{
public *static* int f;

public class Inner
{
public int g;

public Inner()
{
g = f;
}
}
}

"Mortos" <th*****@spammer.co.uk> wrote in message
news:pQ****************@news-binary.blueyonder.co.uk...
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClass;

{Snip}
--
I AM MORTOS!!!! (Switch spammer with blueyonder)

Nov 15 '05 #7
Mortos <th*****@spammer.co.uk> wrote:
I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.


That's because the Java idea of an inner class with an implicit
reference doesn't exist in C#. Nested classes in C# are (mostly) like
static nested classes in Java.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Brad Williams wrote:
C:\projects\testing\wintest1\WinTest1.cs(135): Cannot access a nonstatic
member of outer type 'wintest1.frmWinTest1.Outer' via nested type
'wintest1.frmWinTest1.Outer.Inner'

Indeed, making it static works:


No, it makes it _compile_. It does not solve the problem posted by the
original poster. The behavior in this case is very different.
Nov 15 '05 #9
Mortos wrote:

I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

Julie wrote:
base is what you want:

int intBigClassID = base.ID_BigClass;

{Snip}
--
I AM MORTOS!!!! (Switch spammer with blueyonder)


Duh -- yeah, my mistake.
Nov 15 '05 #10
Julie wrote:

base is what you want:

int intBigClassID = base.ID_BigClass;

Mortos wrote:

I need some quick advice.
I just need to reference a containing class from the nested class. I'm
familiar with Java but C# is proving tricky.

public BigClass {

public int ID_BigClass = -99;

public MiniClass{
public void MiniFunc(){
//How do i get ID_BigClass?
int intBigClassID = {Reference to instance og Big Class}
}
}

}

Thanks
--
I AM MORTOS!!!! (Switch spammer with blueyonder)


Forget that -- I'm a partial idiot today and didn't pay attention to the OP's
question.
Nov 15 '05 #11
Actually it should look like this...

public class BigClass
{
private MiniClass _mini = null;

public BigClass()
{
_mini = new MiniClass(this);
}

public class MiniClass
{
private BigClass _owner = null;

public MiniClass(BigClass owner)
{
_owner = owner;
}
}
}

--
Leit Rachsor
Nov 15 '05 #12
Thanks people for all your responses. These assumptions occasionally
happen for us Java devotees ;-)

I shall amend code as required.

Mortos wrote:
{Snip}

--
I AM MORTOS!!!! (Switch spammer with blueyonder)
Nov 15 '05 #13

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

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
4
by: Sundararajan | last post by:
Dear Folk, I have implemented a nested class as follows: Class Enclosing { string strName; Class Nested1 { }
9
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?...
1
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...
1
by: Diane Yocom | last post by:
I have two nested master pages, each with public properties. From my content page, I am able to access properties from both pages using either Master.PropertyName (for the child master page) or...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
1
by: =?Utf-8?B?QmFybmV5TGlnaHQ=?= | last post by:
Hi, I have a WCF service contract which has message containing a List<MyType> data member where MyType consists of a string Key and object Value. As expected this gets exposed as a MyType...
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.