473,657 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Private" nested classes

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
8 2828
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******** ******@tk2msftn gp13.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
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.WriteLi ne(o.InnerAcces sor.MemberInt);

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

Etienne Boucher
Nov 16 '05 #3

"Etienne Boucher" <et*****@novat. qc.ca> wrote in message news:eR******** ******@tk2msftn gp13.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
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Thank you all for your input.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
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*****@discus sions.microsoft .com> a écrit dans le message de
news:0C******** *************** ***********@mic rosoft.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.WriteLi ne(o.InnerAcces sor.MemberInt);

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

Etienne Boucher

Nov 16 '05 #9

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

Similar topics

10
3314
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble understanding to what extent I should follow this policy in my Python code so I thought I'd ask the group. I read from a previous post that to attain a private-like status of member variables, I should prefix the variable name with two...
5
2013
by: Steven Bethard | last post by:
Philippe C. Martin wrote: > class Debug_Stderr: > __m_text = '' > __m_log_text = None > __m_dbg = None > __m_refresh_count = 0 <rant> I don't see the benefit in 99.9% of cases for making class variables
4
2006
by: BillyMac | last post by:
Hello I emply a "plug-in" architecture for a .NET Windows service. At OnStart, the service browses for DLL's in in sub-folders and loads them dyanamically using Assembly.LoadFrom() The service executable is placed in its own folder with the assemblies required for its successful complilation. Each plugin is also stored in its own folder with the associated assemblies required for its succesful compilation ( the bin/release folder...
1
23186
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not available outside the class). I think I understand Shared (available without having to instantiate a class). So how could a Private Shared Function be called? Why give it this particular scope?
3
6703
by: Jordan Taylor | last post by:
I am confused about protected member functions and objects. Is there any particular advantage of declaring members protected?
4
1868
by: Peter K | last post by:
Hi if I am developing a "class library" which contains some classes which should be exposed to the outside world, and other classes which I really would rather have "private" to my library (helper/utility classes), how do I achieve this? Obviously public classes can just be declared "public". But how do I make classes available only to the other classes which are a part of my library?
16
2043
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I frequently use this for such things as caching, or for keeping track of how many objects a factory function has created, and so on. Today it occurred to me to use a mutable object as the default value of a parameter. A simple example: def...
2
354
by: Jean-Paul Calderone | last post by:
On Thu, 13 Nov 2008 10:58:49 -0800 (PST), rurpy@yahoo.com wrote: Surprise - generators are objects. Jean-Paul
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.