dictionary with multiple object under the same key

  • Thread starter Thread starter Maersa
  • Start date Start date
M

Maersa

Hi All,

Is the a dictionary some place where multiple objects can be collected under
one key ? like...

dictionary.add( "a", "about" );
dictionary.add( "a", "after" );

Took a look at hashtable etc. and others but they don't support it.

thanks,
 
Unless you want to code your own data structure (which I think would be fun,
by the way---even more fun in C# 2.0), then you may just want to do a
hashtable that contains ArrayLists.

When you want to add an item, see if the key exists in the dictionary. If
yes, get the ArrayList out and add the new object. If not, create a new
ArrayList with the new object, and insert it into the Dictionary.

You could even easily wrap this into your own class which handles this step
for you. Removing is pretty straightforward too, although you'll need to
decide if you want to remove an ArrayList completely if it has no more
values in it (depends on what you're using it for, really).

And retreiving a list of values under a key (in this case, an ArrayList) is
fairly simple, unless for some reason you didn't want anyone outside your
class to add values, in which case you'll probably need to create a new
array, use CopyTo, and return it.

Up to you, really.

--Matthew W. Jackson
 
[This followup was posted to microsoft.public.dotnet.framework and a
copy was sent to the cited author.]

Ya, took a look at it,... but in my case "Value" needs to be an object and
not a string.
I would extend a Hash to create a name/object collection.
 
Back
Top