473,326 Members | 2,337 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,326 software developers and data experts.

Nested Classes

Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?
Nov 15 '05 #1
11 2674
AFAIK, no there isn't. Classes that are all members of the same namespace
are 'equal' to each other so Class1 doesn't depend on Class2. However, you
could pull the nested class out and make it it's own class and reference it
accordingly or use inheritance or an interface to accomplish a similar
effect.
"C# Learner" <cs****@learner.here> wrote in message
news:ei********************************@4ax.com...
Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?

Nov 15 '05 #2
"C# Learner" <cs****@learner.here> wrote in message
news:ei********************************@4ax.com...
Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?


Hi C# Learner,

Not in C# v1.0. However, according to the C# v2.0 specification, it will be
possible in C# v2.0.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #3
"William Ryan" <do********@nospam.comcast.net> wrote:
AFAIK, no there isn't. Classes that are all members of the same namespace
are 'equal' to each other so Class1 doesn't depend on Class2. However, you
could pull the nested class out and make it it's own class and reference it
accordingly or use inheritance or an interface to accomplish a similar
effect.


The situation I have in mind is something like this:

I have a class (MyClass) which is getting big (in lines of code) and
I'd like to break it down into parts. Immediately I see where I could
make an "object" out of some of its functionality.

Now, this new object only needs to be known by MyClass instances, and
nowhere else. I don't want this new class to be "visible" to anything
but MyClass.

Therefore, I decide to make it a nested class. However, this nested
class is fairly big itself, and having to declare it within its
"parent" class, in the same file, is a bit of a burden.

For one thing, all methods of the nested class start at an extra
indentation level. For another, the whole file (including MyClass and
its nested class) remains long.

I guess I'll just have to make another class in the same namespace as
MyClass... Though, in my mind, this is a bit of a "hack".

Cheers
Nov 15 '05 #4
"Joe Mayo" <jm***@nospamAtCSharpDashStation.com> wrote:
"C# Learner" <cs****@learner.here> wrote in message
news:ei********************************@4ax.com.. .
Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?
Hi C# Learner,

Not in C# v1.0. However, according to the C# v2.0 specification, it will be
possible in C# v2.0.


Great! Thanks
Joe


Nov 15 '05 #5
C# Learner,

The v2.0 concept is called 'partial' classes, where classes can be spread
over multiple files.

As for what you're working on now, would "internal" work for you? That way,
only classes within your project can see the classes you're building. If
you're worried about creating a dll, for example, where you don't want your
users (i.e., other programmers; consumers of your project) to be able to
access these classes, the internal modifier would be what you would want to
use.

Chris R.

"C# Learner" <cs****@learner.here> wrote in message
news:rp********************************@4ax.com...
"Joe Mayo" <jm***@nospamAtCSharpDashStation.com> wrote:
"C# Learner" <cs****@learner.here> wrote in message
news:ei********************************@4ax.com.. .
Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?


Hi C# Learner,

Not in C# v1.0. However, according to the C# v2.0 specification, it will bepossible in C# v2.0.


Great! Thanks
Joe

Nov 15 '05 #6
"Chris R" <so*****@hotmail.com> wrote:
C# Learner,

The v2.0 concept is called 'partial' classes, where classes can be spread
over multiple files.
I like the sound of that.
As for what you're working on now, would "internal" work for you? That way,
only classes within your project can see the classes you're building. If
you're worried about creating a dll, for example, where you don't want your
users (i.e., other programmers; consumers of your project) to be able to
access these classes, the internal modifier would be what you would want to
use.
Not really, in this case. What I'm looking for is a way to hide the
nested class from *everything* except its "outer" class.

Looks like I'll have to wait for partial classes.
Chris R.


Cheers
Nov 15 '05 #7
Hi C# Learner,

"C# Learner" <cs****@learner.here> wrote in message
news:4b********************************@4ax.com...
"William Ryan" <do********@nospam.comcast.net> wrote: <snip> I guess I'll just have to make another class in the same namespace as
MyClass... Though, in my mind, this is a bit of a "hack".


Just so long as you know that namespaces have no effect on the
visibility/scope of classes.

Regards,
Dan
Nov 15 '05 #8
Hello Learner,

I'm confused by your reticence to use "internal". A class is an
abstraction. An object is an instance of a class, complete with data. One
of the underlying assumptions about OOP is that you should hide the objects,
because that's where the real power lies. Hiding a class is not as
important and leads to some interesting issues (which you've discovered).

Yet you are worried not only about hiding the objects, but also about hiding
the class from other code in the same namespace. This seems
counter-intuitive to me.

From where I sit, this is how it works: The "parent" class creates a private
object of a "child" class type. No other code has access to this particular
object. Sure, other classes can instantiate the child class. (Other
classes can instantiate the parent class too). Unless you are buried under
a serpentine mix of Singleton objects and Factory Methods, you should have
little or no negative consequences to using this idea to reduce the
complexity of your class without sacrificing OOP principles.

puzzled by your puzzlement,
--- Nick

"C# Learner" <cs****@learner.here> wrote in message
news:4b********************************@4ax.com...
"William Ryan" <do********@nospam.comcast.net> wrote:
AFAIK, no there isn't. Classes that are all members of the same namespaceare 'equal' to each other so Class1 doesn't depend on Class2. However, youcould pull the nested class out and make it it's own class and reference itaccordingly or use inheritance or an interface to accomplish a similar
effect.


The situation I have in mind is something like this:

I have a class (MyClass) which is getting big (in lines of code) and
I'd like to break it down into parts. Immediately I see where I could
make an "object" out of some of its functionality.

Now, this new object only needs to be known by MyClass instances, and
nowhere else. I don't want this new class to be "visible" to anything
but MyClass.

Therefore, I decide to make it a nested class. However, this nested
class is fairly big itself, and having to declare it within its
"parent" class, in the same file, is a bit of a burden.

For one thing, all methods of the nested class start at an extra
indentation level. For another, the whole file (including MyClass and
its nested class) remains long.

I guess I'll just have to make another class in the same namespace as
MyClass... Though, in my mind, this is a bit of a "hack".

Cheers

Nov 15 '05 #9
Nick Malik <ni*******@hotmail.nospam.com> wrote:
I'm confused by your reticence to use "internal". A class is an
abstraction. An object is an instance of a class, complete with data. One
of the underlying assumptions about OOP is that you should hide the objects,
because that's where the real power lies. Hiding a class is not as
important and leads to some interesting issues (which you've discovered).

Yet you are worried not only about hiding the objects, but also about hiding
the class from other code in the same namespace. This seems
counter-intuitive to me.


It's not in the same namespace - it's in the same assembly.

I completely understand this, myself. Suppose I have a project which is
a general purpose network library (like Indy :) - it might deal with
NNTP, HTTP and some other protocols. It's nice, IMO, to be able to
insulate the implementation-specific classes of NNTP from those of
HTTP. The HTTP classes may have access to *some* classes which only
other classes in the assembly have access to (in other words, internal
ones) but it would be nice to be able to give further separation than
is currently possible between the HTTP and NNTP implementation classes.

(Java has separation by namespace, .NET has separation by assembly.
Both are nice concepts, and it would be good to have *both*, IMO.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
"Nick Malik" <ni*******@hotmail.nospam.com> wrote:
Hello Learner,

I'm confused by your reticence to use "internal". A class is an
abstraction. An object is an instance of a class, complete with data. One
of the underlying assumptions about OOP is that you should hide the objects,
because that's where the real power lies. Hiding a class is not as
important and leads to some interesting issues (which you've discovered).

Yet you are worried not only about hiding the objects, but also about hiding
the class from other code in the same namespace. This seems
counter-intuitive to me.

From where I sit, this is how it works: The "parent" class creates a private
object of a "child" class type. No other code has access to this particular
object. Sure, other classes can instantiate the child class. (Other
classes can instantiate the parent class too). Unless you are buried under
a serpentine mix of Singleton objects and Factory Methods, you should have
little or no negative consequences to using this idea to reduce the
complexity of your class without sacrificing OOP principles.
Hi Nick,

Let me give an example of what I'm thinking of here:

I have a namespace called FooProtocol. In this namespace is a bunch
of classes which enable the use of a communication protocol called
FooProtocol.

In the FooProtocol namespace I have a class called Session, which
takes care of a client session of FooProtocol. Now, Session reads
packets from the server and fires events after parsing these packets.

In the code of my Session class, I see a lot of methods which have
similar names like:

ProcessFirstPacketType
ProcessSecondPacketType
ProcessThirdPacketType

etc.

Seeing this, I think so myself, I really should make a Processor or
maybe PacketParser class, whose only purpose is to parse these
packets.

Now, this new class should not be visible to anything but Session,
since it is simply an implementation detail of Session.

Further, I may with to create a Processor class as an implementation
detail of another class in the FooProtocol namespace. In this case,
I'd have to name one SessionProcessor and the other
OtherClassNameProcessor, which in my mind shows without doubt that
these classes should be private nested classes of their relevant
classes.
puzzled by your puzzlement,
--- Nick


Hope that makes sense!

Cheers
Nov 15 '05 #11
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:

<snip>
I completely understand this, myself. Suppose I have a project which is
a general purpose network library (like Indy :) - it might deal with
NNTP, HTTP and some other protocols. It's nice, IMO, to be able to
insulate the implementation-specific classes of NNTP from those of
HTTP. The HTTP classes may have access to *some* classes which only
other classes in the assembly have access to (in other words, internal
ones) but it would be nice to be able to give further separation than
is currently possible between the HTTP and NNTP implementation classes.


Yep, that's basically what I'm thinking here.

<snip>

Cheers
Nov 15 '05 #12

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

Similar topics

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:
3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
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: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.