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

  • Thread starter Thread starter Mortos
  • Start date Start date
M

Mortos

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 thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.
base is what you want:

int intBigClassID = base.ID_BigClass;
{Snip}
 
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
 
Mortos said:
I thought 'base' is the parent or superclass of the current class. As
opposed to the 'container'.

{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.
 
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,
 
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 said:
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.
 
Brad said:
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.
 
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;
}
}
}
 
Thanks people for all your responses. These assumptions occasionally
happen for us Java devotees ;-)

I shall amend code as required.

Mortos wrote:
{Snip}
 
Back
Top