Using hashing in collectionbase

  • Thread starter Thread starter myPosts
  • Start date Start date
M

myPosts

Hi all,

I am having class which is derived from collectionbase class.

I am adding some string elements to this collection.

What i want is to add support of hashing into class which i have
derived from collectionbase.

Because currently i am adding strings to my collection and at a time of
removing it is allowing me to removing only according to index and i
want to add functionality of removing through particular key which
should be unique.

Does any one have any idea about this.

Any help will be truely appreciated.

Thanks in advance.
 
My answer would be not to re-invent the wheel; rather than deriving from
CollectionBase, I tend to use encapsulation, i.e. I would have an
instance-field which holds either a Dictionary<string,string> (2.0 onwards)
or a NameValueCollection, which I would then expose through whichever
interfaces and indexers I choose.

If you really want to inherit, neither of these is sealed - but I'd struggle
to see much of a reason (without more info). You might even find that
Dictionary<string,string> or NameValueCollection are enough for your needs
without writing your own class.

Any use?

Marc
 
Hi,

Because currently i am adding strings to my collection and at a time of
removing it is allowing me to removing only according to index and i
want to add functionality of removing through particular key which
should be unique.

How do you associate this key to the string?

You can use any kind of internal storage inside your collection, by default
CollectionBase use an ArrayList , but you can change it as you need.
In your case I would use a HashTable.
Just note that if you do so you will lose the indexing capabilities as a
Hashtable do not assure it.
 
You really don't want a CollectionBase here. What you want to use is a
DictionaryBase, which is used for a map (key/value). A collection is
indexed, and not appropriate here.

Of course, in .NET 2.0, you would use the Dictionary class, and not have
to worry about this.

Hope this helps.
 
Back
Top