470,641 Members | 1,710 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Nested Class

Hi all,

What is the syntax to declare a nested class that can only be
instantiated by the outer class, yet visible to the other classes?
Essentially I want to limit the instantiation and access of the nested
class through the outer class. Thanks for your help in advance.

Nov 17 '05 #1
4 1684
I don't think that's possible. The inner class's constructor must
have at least internal visibility so that the outer class can
instantiate it, but internal visibility allows instantiation by other
classes in the same assembly.

On 8 Nov 2005 07:07:23 -0800, "UncleJoe" <un************@yahoo.ca>
wrote:
Hi all,

What is the syntax to declare a nested class that can only be
instantiated by the outer class, yet visible to the other classes?
Essentially I want to limit the instantiation and access of the nested
class through the outer class. Thanks for your help in advance.

--
http://www.kynosarges.de
Nov 17 '05 #2

"UncleJoe" <un************@yahoo.ca> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi all,

What is the syntax to declare a nested class that can only be
instantiated by the outer class, yet visible to the other classes?
Essentially I want to limit the instantiation and access of the nested
class through the outer class. Thanks for your help in advance.


The best way is to probably define a public interface for your private nested class and the simply
return the interface.

Try this

using System;

class Test
{
public static void Main()
{
Outer outer = new Outer();
IX ix = outer.GetX();
ix.Foo();
}

public interface IX
{
void Foo();
}

public class Outer
{
public IX GetX()
{
return (new Nested());
}

private class Nested : IX
{
public void Foo()
{
Console.WriteLine("in Nested.Foo()");
}
}
}
}
Nov 17 '05 #3
Beautiful! Exactly what I needed. Thanks.

Nov 17 '05 #4
Hi,

UncleJoe wrote:
What is the syntax to declare a nested class that
can only be instantiated by the outer class, yet
visible to the other classes?


You can put the two classes in a separate assembly and declare the Inner
class constructor "internal". If you are creating some kind of a reusable
library already, this could be exactly what you want.

You can also declare a public interface and have your Inner class declared
within the Outer class as private, but implementing the public interface.
With this combination, you can only instantiate objects of the Inner class
within the Outer class, but still expose the objects through the outer class
(as being type of the interface).

The result is more or less what you want, in either case.

--
Chris Priede
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Erik Bongers | last post: by
2 posts views Thread by newbiecpp | last post: by
6 posts views Thread by B0nj | last post: by
8 posts views Thread by Robert W. | last post: by
3 posts views Thread by jdurancomas | last post: by
3 posts views Thread by Cousson, Benoit | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.