PC Review


Reply
Thread Tools Rate Thread

Class in a Class

 
 
Scott M.
Guest
Posts: n/a
 
      12th Aug 2004
What are the advantages of defining a class as part of another class
definition (nesting classes)?


 
Reply With Quote
 
 
 
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      12th Aug 2004
Mainly scope and tidyness. Some classes dont really belong anywhere else.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"Scott M." <s-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What are the advantages of defining a class as part of another class
> definition (nesting classes)?
>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      12th Aug 2004
Scott,
I will nest one class inside another when the nested class is an
implementation detail of the outer class.

For example, the Node class of a LinkedList class. The Node class holds the
actual "links" in the linked list.

Public Class LinkedList

Private Class Node
Public Next As Node
Public Previous As Node
Public Data As Object
End Class

...

End Class

The various methods of LinkedList, such as Add, creates a new Node class and
adds the data to it. The Node class contains the previous & next references
to the other nodes in the list.

Hope this helps
Jay

"Scott M." <s-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What are the advantages of defining a class as part of another class
> definition (nesting classes)?
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      12th Aug 2004
* "Scott M." <s-(E-Mail Removed)> scripsit:
> What are the advantages of defining a class as part of another class
> definition (nesting classes)?


The "advantages" depend on the modifier of the nested class and its
constructor. I suggest to take a look at where nested classes are used
inside the .NET Framework.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      12th Aug 2004
If the nested class is small and used only by the enclosing class - if it
makes sense - and - If the 2 classes are tightly bound
-and- The nested class is not needed by other classes.
less source files

"Scott M." <s-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What are the advantages of defining a class as part of another class
> definition (nesting classes)?
>
>



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004


 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      13th Aug 2004
Why not just make a strongly type collection that holds only "Nodes"?


"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Scott,
> I will nest one class inside another when the nested class is an
> implementation detail of the outer class.
>
> For example, the Node class of a LinkedList class. The Node class holds
> the
> actual "links" in the linked list.
>
> Public Class LinkedList
>
> Private Class Node
> Public Next As Node
> Public Previous As Node
> Public Data As Object
> End Class
>
> ...
>
> End Class
>
> The various methods of LinkedList, such as Add, creates a new Node class
> and
> adds the data to it. The Node class contains the previous & next
> references
> to the other nodes in the list.
>
> Hope this helps
> Jay
>
> "Scott M." <s-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> What are the advantages of defining a class as part of another class
>> definition (nesting classes)?
>>
>>

>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      13th Aug 2004
Where might those nested classes be in the .NET Framework? How would I find
them?


"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>* "Scott M." <s-(E-Mail Removed)> scripsit:
>> What are the advantages of defining a class as part of another class
>> definition (nesting classes)?

>
> The "advantages" depend on the modifier of the nested class and its
> constructor. I suggest to take a look at where nested classes are used
> inside the .NET Framework.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      13th Aug 2004
Why would there be less source files since non-nested classes can be written
into one source module?


"Hal Rosser" <(E-Mail Removed)> wrote in message
news:m%QSc.456$(E-Mail Removed)...
> If the nested class is small and used only by the enclosing class - if it
> makes sense - and - If the 2 classes are tightly bound
> -and- The nested class is not needed by other classes.
> less source files
>
> "Scott M." <s-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> What are the advantages of defining a class as part of another class
>> definition (nesting classes)?
>>
>>

>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004
>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      13th Aug 2004
Scott,
> Why not just make a strongly type collection that holds only "Nodes"?

Because a strongly typed collection of Nodes is NOT a LinkedList! (CS 101)

First consider that you have a Widget class. You could create:
- an array (either an array or ArrayList) of Widgets
- a dictionary (HashTable) of Widgets
- a linked list of Widgets
- a strongly typed collection of Widgets (CollectionBase)
- a strongly typed dictionary of Widgets (DictionaryBase)
- a strongly typed linked list of Widgets

The above list excludes the fact there are binary trees, balanced trees,
graphs, and other "collections" that could hold & organize Widgets. It also
avoids the fact that you could have a keyed linked list or a non-keyed
linked list.

Within .NET most people create strongly typed collections with
CollectionBase (which is based on an ArrayList aka an Array) or
DictionaryBase (which is based on an Hashtable aka a dictionary). I hope you
remember that a LinkedList has performance characteristics that are
different then an array or a dictionary, meaning that a linked list may be
better in some cases. For example inserting into a linked listed is normally
faster then inserting into an array, while searching a sorted array (binary
search) is normally faster then searching a linked list (linear search).

Further you could have a single linked list or a double linked list.

Implementing LinkedList with a private Node class means that you do not need
to add Previous & Next fields to the Widget class itself, allowing your
LinkedList class to be reused for Wingdings in addition to Widgets. You
could even design LinkedList to be a base class, ala CollectionBase,
allowing you to create strongly typed linked lists!

When you are implementing the linted list of Widgets, you would use the Node
class to hold each Widget linking it to the next Widget. Only the Linked
List class itself needs to know about the Node class itself, hence you nest
it inside of LinkedList.

By hiding the details of Node within LinkedList you as a consumer of the
LinkedList only need to worry about adding, removing, inserting, and
indexing Widget objects, as you don't really care or need to know that a
Node object is how your Widgets themselves are organized internally to the
LinkedList, as Node is an implementation detail!

Note .NET has System.Collections.Specialized.ListDictionary &
System.Collections.Specialized.HybridDictionary that offer LinkedList
functionality. I use HybridDictionary in a few places where I need the
performance of a linked list for small lists, and the performance of a
hashtable for longer lists. HybridDictionary internally changes from a
ListDictionary to a Hashtable based on the number of elements in the
HybridDictionary.

System.Collections.Specialized.NameObjectCollectionBase.KeysCollection is an
example of a nested class, as KeysCollection is tied closes to
NameObjectCollectionBase, yet is publicly usable.

Hope this helps
Jay

"Scott M." <s-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Why not just make a strongly type collection that holds only "Nodes"?
>
>
> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Scott,
> > I will nest one class inside another when the nested class is an
> > implementation detail of the outer class.
> >
> > For example, the Node class of a LinkedList class. The Node class holds
> > the
> > actual "links" in the linked list.
> >
> > Public Class LinkedList
> >
> > Private Class Node
> > Public Next As Node
> > Public Previous As Node
> > Public Data As Object
> > End Class
> >
> > ...
> >
> > End Class
> >
> > The various methods of LinkedList, such as Add, creates a new Node class
> > and
> > adds the data to it. The Node class contains the previous & next
> > references
> > to the other nodes in the list.
> >
> > Hope this helps
> > Jay
> >
> > "Scott M." <s-(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> What are the advantages of defining a class as part of another class
> >> definition (nesting classes)?
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      13th Aug 2004

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Scott,
>> Why not just make a strongly type collection that holds only "Nodes"?

> Because a strongly typed collection of Nodes is NOT a LinkedList! (CS 101)


Ok Jay...breathe a little buddy!

So, would it be fair to say that nested classes are for very specific
situations and not extremely mainstream?

>
> First consider that you have a Widget class. You could create:
> - an array (either an array or ArrayList) of Widgets
> - a dictionary (HashTable) of Widgets
> - a linked list of Widgets
> - a strongly typed collection of Widgets (CollectionBase)
> - a strongly typed dictionary of Widgets (DictionaryBase)
> - a strongly typed linked list of Widgets
>
> The above list excludes the fact there are binary trees, balanced trees,
> graphs, and other "collections" that could hold & organize Widgets. It
> also
> avoids the fact that you could have a keyed linked list or a non-keyed
> linked list.
>
> Within .NET most people create strongly typed collections with
> CollectionBase (which is based on an ArrayList aka an Array) or
> DictionaryBase (which is based on an Hashtable aka a dictionary). I hope
> you
> remember that a LinkedList has performance characteristics that are
> different then an array or a dictionary, meaning that a linked list may be
> better in some cases. For example inserting into a linked listed is
> normally
> faster then inserting into an array, while searching a sorted array
> (binary
> search) is normally faster then searching a linked list (linear search).
>
> Further you could have a single linked list or a double linked list.
>
> Implementing LinkedList with a private Node class means that you do not
> need
> to add Previous & Next fields to the Widget class itself, allowing your
> LinkedList class to be reused for Wingdings in addition to Widgets. You
> could even design LinkedList to be a base class, ala CollectionBase,
> allowing you to create strongly typed linked lists!
>
> When you are implementing the linted list of Widgets, you would use the
> Node
> class to hold each Widget linking it to the next Widget. Only the Linked
> List class itself needs to know about the Node class itself, hence you
> nest
> it inside of LinkedList.
>
> By hiding the details of Node within LinkedList you as a consumer of the
> LinkedList only need to worry about adding, removing, inserting, and
> indexing Widget objects, as you don't really care or need to know that a
> Node object is how your Widgets themselves are organized internally to the
> LinkedList, as Node is an implementation detail!
>
> Note .NET has System.Collections.Specialized.ListDictionary &
> System.Collections.Specialized.HybridDictionary that offer LinkedList
> functionality. I use HybridDictionary in a few places where I need the
> performance of a linked list for small lists, and the performance of a
> hashtable for longer lists. HybridDictionary internally changes from a
> ListDictionary to a Hashtable based on the number of elements in the
> HybridDictionary.
>
> System.Collections.Specialized.NameObjectCollectionBase.KeysCollection is
> an
> example of a nested class, as KeysCollection is tied closes to
> NameObjectCollectionBase, yet is publicly usable.
>
> Hope this helps
> Jay
>
> "Scott M." <s-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Why not just make a strongly type collection that holds only "Nodes"?
>>
>>
>> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > Scott,
>> > I will nest one class inside another when the nested class is an
>> > implementation detail of the outer class.
>> >
>> > For example, the Node class of a LinkedList class. The Node class holds
>> > the
>> > actual "links" in the linked list.
>> >
>> > Public Class LinkedList
>> >
>> > Private Class Node
>> > Public Next As Node
>> > Public Previous As Node
>> > Public Data As Object
>> > End Class
>> >
>> > ...
>> >
>> > End Class
>> >
>> > The various methods of LinkedList, such as Add, creates a new Node
>> > class
>> > and
>> > adds the data to it. The Node class contains the previous & next
>> > references
>> > to the other nodes in the list.
>> >
>> > Hope this helps
>> > Jay
>> >
>> > "Scott M." <s-(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> >> What are the advantages of defining a class as part of another class
>> >> definition (nesting classes)?
>> >>
>> >>
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't specify a location for new class code file on class diagram>new class dialog? Andy B Microsoft C# .NET 3 14th Feb 2008 01:57 AM
call a mnuFileNew_Click method from a Main MDI class from another class (can be a MdiChild class.. )? M. G, Microsoft Dot NET Framework Forms 1 31st May 2006 06:28 AM
Can it possible redefine class with subclass when inherit base class with sub class? ABC Microsoft C# .NET 4 10th Jan 2006 11:13 PM
Pass an instantiated base class to a new class that inherits the same class -- Possible?? Brad Navarro Microsoft C# .NET 3 9th Jan 2004 01:07 AM
activex ,money tree dialer,RdxE Class,updaate class,YInstStarter Class damaged RUSSELL Windows XP Performance 1 23rd Nov 2003 02:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:16 AM.