473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Create a custom collection

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
Jan 10 '06 #1
6 2680
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

Jan 10 '06 #2
| 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/de...classtopic.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
"Mike" <mi**@hitnext.com> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
| 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
|
|
Jan 10 '06 #3
Thanks guys, i may try you suggestion.
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:uQ**************@TK2MSFTNGP12.phx.gbl...
| 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/de...classtopic.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
"Mike" <mi**@hitnext.com> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
| 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
|
|

Jan 10 '06 #4
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

"Mike" <mi**@hitnext.com> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
I implemented a class called MyDictionary that inherits from
DictionaryBase.

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

Jan 10 '06 #5
Hi jim!

what do you mean by;
Then in my custom Objects, add a Key property and an ID property

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

Thanks

"Jim Wooley" <jw******@bellsouth.net> wrote in message
news:eI*************@TK2MSFTNGP12.phx.gbl...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

"Mike" <mi**@hitnext.com> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
I implemented a class called MyDictionary that inherits from
DictionaryBase.

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


Jan 10 '06 #6
Mike,

I did not try it, however I got the idea that it was worth a try to use the
sortedlist and to switch your key and your item.

http://msdn.microsoft.com/library/de...classtopic.asp

Just to try

Cor
Jan 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the...
0
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). ...
1
by: Howard Weiss | last post by:
I would like to create a collection of managed objects - to be exact, instances of a form which I use to display images. This collection would be similar to the controls collection which is part...
0
by: Lloyd Dupont | last post by:
I'm trying to create a templated custom control which hold a collection of something else in his property 'Panels' when this property is of type ArrayList it parse ok. however when I try to use...
1
by: Jeff S | last post by:
I'm storing a list of widgets in a database. The list changes infrequently (twice per week at most), and is relatively short (200 items at most, with very little detail per item). A small subset of...
1
by: dx | last post by:
I'd appreciate some ideas or direction with a collection issue. I need to create a custom collection that inherits ICollection, IEnumberable (need to be able to access by key, index and be able...
1
by: dx | last post by:
I'd appreciate some ideas or direction with a collection issue. I need to create a custom collection that inherits ICollection, IEnumberable (need to be able to access by key, index and be able...
2
by: Don | last post by:
I've been trying to create a custom collection that can be bound to a combobox via the DataSource property, but I can't seem to get it to work. I've created a class that implements IList, and it...
6
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.