|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Create a custom collectionI 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 Hi Mike,
> Dim md as New MyDictionary I suppose that you mean at run-time not a compile-time...> 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 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 | I implemented a class called MyDictionary that inherits from Have you tried inheriting from DictionaryBase. | The thing is that my class, cannot handle a duplicate value in the "keys" System.Collections.Specialized.NameValueCollection? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsspecializednamevaluecollectionclasstopic.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? -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Mike" <m***@hitnext.com> wrote in message news:OF9fzCgFGHA.2652@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 | | Thanks guys, i may try you suggestion.
Show quoteHide quote "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in message news:uQgjTEhFGHA.3936@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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsspecializednamevaluecollectionclasstopic.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" <m***@hitnext.com> wrote in message > news:OF9fzCgFGHA.2652@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 > | > | > > 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 Show quoteHide quote "Mike" <m***@hitnext.com> wrote in message news:OF9fzCgFGHA.2652@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" 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 Show quoteHide quote "Jim Wooley" <jwool***@bellsouth.net> wrote in message news:eIKayLjFGHA.752@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" <m***@hitnext.com> wrote in message > news:OF9fzCgFGHA.2652@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" > > 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionssortedlistclasstopic.asp Just to try Cor
VB2005 - Stop User from Leaving Row in DataGridView
How to re-acquire the Text from a combo box Anyone used Socketwrench? Problem With Insert into MySQL DB using VB.Net 2005 Sending E-Mail from User's Machine What mvp use for printing Random String labels Accessing a scanner programmatically VS.Net 2003 |
|||||||||||||||||||||||