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

"Private" nested classes

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Make the nested class' declaration 'private' - that should have the effect
you desire.

Richard

"Etienne Boucher" <et*****@novat.qc.ca> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2

P: n/a
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
Nov 16 '05 #3

P: n/a

"Etienne Boucher" <et*****@novat.qc.ca> wrote in message news:eR**************@tk2msftngp13.phx.gbl...
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
Nov 16 '05 #4

P: n/a
Hans Kesting <ne***********@spamgourmet.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
Hans Kesting <ne***********@spamgourmet.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Ah, sorry.

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

Hans Kesting
Nov 16 '05 #6

P: n/a
Hans Kesting <ne***********@spamgourmet.com> wrote:
He could use "internal" then: then you block at least classes from other
assemblies.


Indeed - that's probably the best solution here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
Thank you all for your input.

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de
news:MP************************@msnews.microsoft.c om...
Hans Kesting <ne***********@spamgourmet.com> wrote:
He could use "internal" then: then you block at least classes from other
assemblies.


Indeed - that's probably the best solution here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8

P: n/a
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" <Mi*****@discussions.microsoft.com> a écrit dans le message de
news:0C**********************************@microsof t.com...
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) Wouldn't making the inner constructor private solved the problem though?

"Etienne Boucher" wrote:
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

Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.