By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.