"Private" nested classes

  • Thread starter Thread starter Etienne Boucher
  • Start date Start date
E

Etienne Boucher

Nested classes are usualy objects made to only live in their parent object
instance. In other words...

public class Outter
{
public class Inner
{
}
}

Prevent being able to use "new Outter.Inner()", in Main for exemple, while
still being able to access the members of Inner.

This could be done with a public interface implemented in a private nested
class, or like this:

public class Outter
{
private Inner i;

public Outter()
{
new Inner(this);
}

public class Inner
{
private Outter o;

public Inner(Outter o)
{
if (null != o.i)
throw new Exception();
o.i = this;
this.o = o;
}
}
}

But that's not very clean. Any one can think of a better way?

Etienne Boucher
 
Make the nested class' declaration 'private' - that should have the effect
you desire.

Richard
 
The goal is to still be able to access the members of the nested type from
outside the outter class. From the definition

public class Outter
{
private Inner i;

public Outter()
{
new Inner(this);
}

public Inner InnerAccessor
{
get { return i; }
}

public class Inner
{
private Outter o;
public int MemberInt;

public Inner(Outter o)
{
if (null != o.i)
throw new Exception();
o.i = this;
this.o = o;
}
}
}

The following code can still be exectuted.

Outter o = new Outter();
Console.WriteLine(o.InnerAccessor.MemberInt);

Again, if anyone knows a better way to do this.

Etienne Boucher
 
Etienne Boucher said:
Nested classes are usualy objects made to only live in their parent object
instance. In other words...

public class Outter
{
public class Inner
{
}
}

Prevent being able to use "new Outter.Inner()", in Main for exemple, while
still being able to access the members of Inner.

use a private constructor. Outer can still access it to make a new
instance of Inner, but noone else can. As the inner class is public, it's
methods are still available.

Hans Kesting
 
Hans Kesting said:
use a private constructor. Outer can still access it to make a new
instance of Inner, but noone else can.

Nope - an outer class can't use private members of the nested class,
only the other way round.
 
Jon Skeet said:
Nope - an outer class can't use private members of the nested class,
only the other way round.

Ah, sorry.

He could use "internal" then: then you block at least classes from other
assemblies.

Hans Kesting
 
Hans Kesting said:
He could use "internal" then: then you block at least classes from other
assemblies.

Indeed - that's probably the best solution here.
 
I'm also missing my old friend the friend statement.That's one thing that's
missing in C#, I guess we'll have to live with nested classes and friend
assemblies.

Etienne Boucher

Mikolas said:
The perfect solution would probably be making the constructor of the
internal class visible only to the outter class. I'm not aware of a
technique that would enable that. Or in c++ there's the friend keyword,
which hasn't been adopted into C#(as far as I now)
 
Back
Top