|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Generic collection questionI am doing a Generic.Dictionary(of string,myobject)
I need to access the item by key, but also by index. Is this the wrong thing to use? Should I be using something different. In other words sometimes I just need item(0), the first item added to the list, but other times by key. TIA, Shane To access the keys by index, use the following code:
Dim KeyArray(YourDictionary.Keys.Count) as string YourDictionary.Keys.CopyTo(KeyArray) ValueByIndex = YourDictionary(KeyArray(Index)) Show quoteHide quote > I am doing a Generic.Dictionary(of string,myobject) > > I need to access the item by key, but also by index. Is this the wrong thing > to use? Should I be using something different. > > In other words sometimes I just need item(0), the first item added to the > list, but other times by key. > > TIA, > > Shane > > Well,
I was reading the documentation which says there is no way to guarantee the order. I need item 0. I am now trying to build my own generic class to do this. Thanks, Shane Show quoteHide quote "Theo Verweij" <tverw***@xs4all.nl> wrote in message news:%231krgzHyGHA.3440@TK2MSFTNGP06.phx.gbl... > To access the keys by index, use the following code: > > Dim KeyArray(YourDictionary.Keys.Count) as string > YourDictionary.Keys.CopyTo(KeyArray) > > ValueByIndex = YourDictionary(KeyArray(Index)) > > >> I am doing a Generic.Dictionary(of string,myobject) >> >> I need to access the item by key, but also by index. Is this the wrong >> thing to use? Should I be using something different. >> >> In other words sometimes I just need item(0), the first item added to the >> list, but other times by key. >> >> TIA, >> >> Shane Shane,
You might be able to build off of NameObjectCollectionBase. Kerry Moorman Show quoteHide quote "SStory" wrote: > Well, > > I was reading the documentation which says there is no way to guarantee the > order. > I need item 0. > > I am now trying to build my own generic class to do this. > > Thanks, > > Shane > > "Theo Verweij" <tverw***@xs4all.nl> wrote in message > news:%231krgzHyGHA.3440@TK2MSFTNGP06.phx.gbl... > > To access the keys by index, use the following code: > > > > Dim KeyArray(YourDictionary.Keys.Count) as string > > YourDictionary.Keys.CopyTo(KeyArray) > > > > ValueByIndex = YourDictionary(KeyArray(Index)) > > > > > >> I am doing a Generic.Dictionary(of string,myobject) > >> > >> I need to access the item by key, but also by index. Is this the wrong > >> thing to use? Should I be using something different. > >> > >> In other words sometimes I just need item(0), the first item added to the > >> list, but other times by key. > >> > >> TIA, > >> > >> Shane > > > That's what I did and finally got it going.
For any who read this.. BE WARE that For Each doesn't work well with it unless you override the GetEnumerator and return the values.GetEnumerator This will make it work as you would expect. Otherwise you get an enumeration of the keys. Thanks to all! Shane Show quoteHide quote "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message news:A4E28518-C279-41FD-8C03-B48217FFA742@microsoft.com... > Shane, > > You might be able to build off of NameObjectCollectionBase. > > Kerry Moorman > > > "SStory" wrote: > >> Well, >> >> I was reading the documentation which says there is no way to guarantee >> the >> order. >> I need item 0. >> >> I am now trying to build my own generic class to do this. >> >> Thanks, >> >> Shane >> >> "Theo Verweij" <tverw***@xs4all.nl> wrote in message >> news:%231krgzHyGHA.3440@TK2MSFTNGP06.phx.gbl... >> > To access the keys by index, use the following code: >> > >> > Dim KeyArray(YourDictionary.Keys.Count) as string >> > YourDictionary.Keys.CopyTo(KeyArray) >> > >> > ValueByIndex = YourDictionary(KeyArray(Index)) >> > >> > >> >> I am doing a Generic.Dictionary(of string,myobject) >> >> >> >> I need to access the item by key, but also by index. Is this the wrong >> >> thing to use? Should I be using something different. >> >> >> >> In other words sometimes I just need item(0), the first item added to >> >> the >> >> list, but other times by key. >> >> >> >> TIA, >> >> >> >> Shane >> >> >> I just runned some tests, and it seems that the order stays in the order
you added the items, as long as you don't use the remove method. SStory wrote: Show quoteHide quote > Well, > > I was reading the documentation which says there is no way to guarantee the > order. > I need item 0. > > I am now trying to build my own generic class to do this. > > Thanks, > > Shane > > "Theo Verweij" <tverw***@xs4all.nl> wrote in message > news:%231krgzHyGHA.3440@TK2MSFTNGP06.phx.gbl... >> To access the keys by index, use the following code: >> >> Dim KeyArray(YourDictionary.Keys.Count) as string >> YourDictionary.Keys.CopyTo(KeyArray) >> >> ValueByIndex = YourDictionary(KeyArray(Index)) >> >> >>> I am doing a Generic.Dictionary(of string,myobject) >>> >>> I need to access the item by key, but also by index. Is this the wrong >>> thing to use? Should I be using something different. >>> >>> In other words sometimes I just need item(0), the first item added to the >>> list, but other times by key. >>> >>> TIA, >>> >>> Shane > > SStory,
If I need a generic collection with access by key & index I will normally use a System.Collections.ObjectModel.KeyedCollection(Of TKey, TItem). Especially when used as a base class. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "SStory" <nospam@nospam.com> wrote in message news:e5wW7jHyGHA.1936@TK2MSFTNGP06.phx.gbl... |I am doing a Generic.Dictionary(of string,myobject) | | I need to access the item by key, but also by index. Is this the wrong thing | to use? Should I be using something different. | | In other words sometimes I just need item(0), the first item added to the | list, but other times by key. | | TIA, | | Shane | | Thanks Jay
Show quoteHide quote "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in message news:ueEjYsKyGHA.4232@TK2MSFTNGP05.phx.gbl... > SStory, > If I need a generic collection with access by key & index I will normally > use a System.Collections.ObjectModel.KeyedCollection(Of TKey, TItem). > Especially when used as a base class. > > -- > Hope this helps > Jay B. Harlow [MVP - Outlook] > .NET Application Architect, Enthusiast, & Evangelist > T.S. Bradley - http://www.tsbradley.net > > > "SStory" <nospam@nospam.com> wrote in message > news:e5wW7jHyGHA.1936@TK2MSFTNGP06.phx.gbl... > |I am doing a Generic.Dictionary(of string,myobject) > | > | I need to access the item by key, but also by index. Is this the wrong > thing > | to use? Should I be using something different. > | > | In other words sometimes I just need item(0), the first item added to > the > | list, but other times by key. > | > | TIA, > | > | Shane > | > | > >
Saving changes made in DataGridView
Atomic Operation? how to force a 'SyncWithCurrencyManager' update hex to unicode: problem One Last Class Question - Example that works Current namespace, stack and lineno How to make double click event to call and execute the mouseup event code how to make a mouseup event called only once during a double click event? DefaultValueAttribute And Array Properties Strange float behaviour |
|||||||||||||||||||||||