nested classes, protection levels, and no friend in c#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi...

Maybe I'm just being especially obtuse today, but I've been perplexed by how
some of the protection levels have been working with nested classes in C#. I
have a parent class that wants to be able define sub-object to return to
consumers but wants to really clamp down on how much is exposed to the
consumer - for example, I wanted to make consumers unable to instantiate the
sub-objects on their own.

Originally, i'd hoped something like
public class a {
public class b {
private or protected b() { //constructor }
...
}
....
}

would allow only the parent class to invoke the a.b constructor, but that
doesn't appear to be the case. In the protection level table I found on
MSDN, it just didn't look like protection levels addressed the concept of a
parent class restricting access to a child class directly.

Then I went looking for an old C++ construct to try - the friend class
declaration - and I found C# didn't have it. If I could declare a.b's
constructor private and then declare a a friend class of a.b that might do
what I was getting at.

Am I just missing some point here?

Thanks
_mark
 
Mark,

The closest thing that C# has to "friend" is internal, meaning that only
other classes in the same assembly can access that class. You can declare
the constructor as internal, but it might not be what you are looking for (I
don't know what scope is acceptable for you).

If you absolutely positively can only have your class created by the
parent class, then you will have to take this class and put it by itself in
it's own assembly, and then declare the constructor as internal.

Hope this helps.
 
The major problem you will run into with what you are describing is that the
sub class that is returned needs to be visible enough to the code calling the
function that returns it to be seen... one way around this would be to use an
interface, something like this:

public class ParentClass
{
public interface ISubClass
{
void Func1();
int Func2();
}

public ISubClass GetSubClass()
{
return new SubClass();
}

private class SubClass : ISubClass
{
public void Func1()
{

}
public int Func2()
{
return 42;
}
}
}

and later:

ParentClass parent = new ParentClass();
ParentClass.ISubClass sub = parent.GetSubClass();
sub.Func1();

Under this method, the internal class you do not want getting out is created
and returned, however the actual constructor is cut off from the caller,
instead they are only given a reference to the instance with the same
interface.

Brendan
 
namespace myNamespace
{
public class UserCannotCreate
{
internal UserCannotCreate() { }
// other stuff
}

public class CreatesOBject
{
public UserCannotCreate GetChildObject()
{
return(new UserCannotCreate());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top