Which collection object for this simple job?

  • Thread starter Thread starter BH
  • Start date Start date
B

BH

Hi all

just learning about .NET list/collections and I'm confused about which type
is best for which job.

Say I have a simple object with 4 data members: ID (an integer), Name
(string), ParentID (integer), and HasChildren (boolean).
This object represents a single member of a hierarchical list.

I'd like to put those objects into a lean and mean .NET collection( or array
or hashtable) that has only a few purposes in life...
1) search for all the objects within that collection where ParentID == 18456
2) copy the set of matching objects to *another* collection for further
usage
3) copy the Name text of each matching object to another widget somewhere
(TreeView or whatever)

Note: For reasons too long to mention here, I need to stay away from things
like DataSets and DataTables.
Seems like a simple exercise, but I'm lost in the forest of various .NET
list-related objects...hard to find info that summarizes what's best for
what kind of jobs.

Any assistance --with direct solution ideas or help/resources/tutorials for
collections -- greatly appreciated.
 
Use an IDictionary instance (Hashtable might work for you). Inside that,
you will place multiple ArrayList instances, keyed on the ParentID. Inside
each ArrayList instance you will place multiple instances of your simple
object.

IDictionary ==(ParentID)==> IList =======> SimpleObject

Alternately, you could replace the IList with a SortedArray (which is an
IDictionary, believe it or not, not an IList) keyed on the Name. This will
return the objects to you in alphabetical order when you enumerate over the
collection.
 
Hi all

just learning about .NET list/collections and I'm confused about which
type is best for which job.

Say I have a simple object with 4 data members: ID (an integer), Name
(string), ParentID (integer), and HasChildren (boolean).
This object represents a single member of a hierarchical list.

I'd like to put those objects into a lean and mean .NET collection( or
array or hashtable) that has only a few purposes in life...
1) search for all the objects within that collection where ParentID ==
18456 2) copy the set of matching objects to *another* collection for
further usage
3) copy the Name text of each matching object to another widget
somewhere (TreeView or whatever)

Note: For reasons too long to mention here, I need to stay away from
things like DataSets and DataTables.
Seems like a simple exercise, but I'm lost in the forest of various
.NET list-related objects...hard to find info that summarizes what's
best for what kind of jobs.

Any assistance --with direct solution ideas or
help/resources/tutorials for collections -- greatly appreciated.


Help files for ICollection:
========================================================================
"The ICollection interface is the base interface for classes in the
System.Collections namespace.

IDictionary and IList are more specialized interfaces that are based on
the ICollection interface. An IDictionary implementation is a collection
of key-and-value pairs, like the Hashtable class. An IList implementation
is a collection of values that can be sorted and whose members can be
accessed by index, like the ArrayList class.

Some collections that limit access to their elements, like the Queue
class and the Stack class, directly implement the ICollection interface.

If neither the IDictionary interface nor the IList interface meet the
requirements of the required collection, derive the new collection class
from the ICollection interface instead for more flexibility."
========================================================================

If you want to be able to search the collection FAST for a particular
value, then you need a collection that implements IDictionary. An
example would be HashTable.

If you need the collection to recall items in any particular order then
you don't want IDictionary, but instead IList. An example would be
ArrayList.

Other built in collections derive from ICollection. Examples SortedList,
Queue, Stack.

If you need the advantages of both IDictionary and IList, then you need
to make your own hybrid. Inherit from CollectionBase for a good start.
I demonstrate how to do this in the "strongly typed" collection templates
that come with the "simple code generator" in my signature. The release
notes for the download describe each templates purpose. The templates I
provide expect either a string or GUID unique value on the class type
contained in the collection. For integer you'll have to make a few minor
modifications. Let me know if you have trouble doing this.


As far as copying an object from one collection to another, implement a
DeepCopy or Clone method (ICloneable interface) on your collection.
Sorry that is not in my current samples.

You shouldn't couple your class directly to the Windows Forms dll by
having a method of which the purpose is to create a TreeNode and set
properties. Instead, in your form define a method that takes your custom
collection and fills the treeview and sets visual properties as
appropriate.


Let me know if you have any further questions and if the provided link is
helpful.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)
 
Back
Top