473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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

Nov 17 '05 #1
3 3766
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark" <mm******@nospam.nospam> wrote in message
news:83**********************************@microsof t.com...
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

Nov 17 '05 #2
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
"Mark" wrote:
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

Nov 17 '05 #3
namespace myNamespace
{
public class UserCannotCreate
{
internal UserCannotCreate() { }
// other stuff
}

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

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"Mark" wrote:
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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Stippler | last post by:
Hi what's wrong about the following code? It does not compile. template <typename T> class A { public: class B; };
2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
7
by: newbiecpp | last post by:
Why this code cannot be compiled (under VC++ 7.0): class Outer { public: class Inner { public: Inner() : in_data(0) {} void action() { Outer::out_data++; // illegal reference to...
5
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int...
7
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Etienne Boucher | last post by:
Nested classes are usualy objects made to only live in their parent object instance. In other words... public class Outter { public class Inner { } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.