Create a custom collection

M

Mike

Hello guys, how are you?



I implemented a class called MyDictionary that inherits from DictionaryBase.

The thing is that my class, cannot handle a duplicate value in the "keys"



Like;



Dim md as New MyDictionary

md.add("Mike", Object1)

md.add("Mike", Object2)



This will generate an error during compilation, because I cannot add Mike
two times to the collection, we all know that.

I need to come up with something (like a custom made collection or
structure) that allow me to do this.

I need a key (string type, can be duplicate) and a value (object type, like
an instance of another class).



Could you please help me on this, any example will be highly appreciated!



Mike
 
C

Carlos J. Quintero [VB MVP]

Hi Mike,
Dim md as New MyDictionary
md.add("Mike", Object1)
md.add("Mike", Object2)
This will generate an error during compilation, because I cannot add Mike
two times to the collection, we all know that.

I suppose that you mean at run-time not a compile-time...
I need a key (string type, can be duplicate) and a value

Dictionaries use keys and keys, by definition, can not be duplicated.
Otherwise, when you try to retrieve an item by key, which one of the
duplicates you would return?

Maybe you would have to use a dictionary whose objects are collections
(allowing duplicates)...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio 2005, Visual Studio .NET,
VB6, VB5 and VBA
You can code, design and document much faster in VB.NET, C#, C++ or VJ#
Free resources for add-in developers:
http://www.mztools.com
 
J

Jay B. Harlow [MVP - Outlook]

| I implemented a class called MyDictionary that inherits from
DictionaryBase.
| The thing is that my class, cannot handle a duplicate value in the "keys"

Have you tried inheriting from
System.Collections.Specialized.NameValueCollection?

http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp

Unfortunately it wants strings & not objects.

You could code your Add such that if you are adding a duplicate that it
changes that entry to a list and adds the duplicates to the list.
Unfortunately this complicates the indexer's & enumerators as sometimes you
are returning a single object, sometimes multiple objects.

Have you considered defining MyDictionary to be a dictionary of an array of
your objects? Where each key holds an array (ArrayList?) of one or more
objects?


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello guys, how are you?
|
|
|
| I implemented a class called MyDictionary that inherits from
DictionaryBase.
|
| The thing is that my class, cannot handle a duplicate value in the "keys"
|
|
|
| Like;
|
|
|
| Dim md as New MyDictionary
|
| md.add("Mike", Object1)
|
| md.add("Mike", Object2)
|
|
|
| This will generate an error during compilation, because I cannot add Mike
| two times to the collection, we all know that.
|
| I need to come up with something (like a custom made collection or
| structure) that allow me to do this.
|
| I need a key (string type, can be duplicate) and a value (object type,
like
| an instance of another class).
|
|
|
| Could you please help me on this, any example will be highly appreciated!
|
|
|
| Mike
|
|
 
J

Jim Wooley

I agree with the other responses that a dictionary may not be what you want.
I would typically solve this using a class that inherits from CollectionBase
or ReadOnlyCollectionBase (depending on the business needs). Then in my
custom Objects, add a Key property and an ID property. The ID property would
expose a GUID assigned by the BO to allow for unique searching for the
purposes of the Item and Remove methods of the collection. This will allow
you to bind to the "Key" property and have multiple objects with the same
"Key" (the ID will be different however).

Jim
 
M

Mike

Hi jim!

what do you mean by;

Whats a custom object for you, a class with a key, ID and value memebers?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top