Re: Class in a Class

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 08/13/04


Date: Fri, 13 Aug 2004 10:17:42 -0500

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-mar@nospam.nospam> wrote in message
news:OG2qCIUgEHA.1972@TK2MSFTNGP09.phx.gbl...
> Why not just make a strongly type collection that holds only "Nodes"?
>
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
> news:OR0nxhIgEHA.536@TK2MSFTNGP11.phx.gbl...
> > 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-mar@nospam.nospam> wrote in message
> > news:OCseaQIgEHA.536@TK2MSFTNGP11.phx.gbl...
> >> What are the advantages of defining a class as part of another class
> >> definition (nesting classes)?
> >>
> >>
> >
> >
>
>



Relevant Pages

  • Re: Class in a Class
    ... As I stated in my original post, nested classes are primarily ... for implementation details. ... >> LinkedList class to be reused for Wingdings in addition to Widgets. ... >> allowing you to create strongly typed linked lists! ...
    (microsoft.public.dotnet.languages.vb)
  • saving state via
    ... I am trying to find a 'best practice' method for saving / loading ... saving my application state as an array: ... "bad window path name" no matter what I pass in to it. ... Subject: tcl/tk:saving state of widgets?? ...
    (comp.lang.tcl)
  • Problem when switching from SoapClient to WebServicesClientProtocol
    ... I have a working C# class library that acts as a soap client. ... over the formatting of the soap body: ... It all boils down to the "this.invoke" method requiring an object array ... //populate the test widgets array ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • array of Tkinter variables?
    ... I was hoping that I could use an array ... of StringVar variables to attach to these widgets, ... The University of Texas at Austin ...
    (comp.lang.python)
  • Re: ttk::checkbutton - I think, its a bug
    ... value for those widgets that implement the getter. ... linked to an array entry (with a dedicated array for ... for a long time whereby it basically did not work ...
    (comp.lang.tcl)

Loading