Home All Groups Group Topic Archive Search About

Generic collection question

Author
25 Aug 2006 7:03 PM
SStory
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

Author
25 Aug 2006 7:30 PM
Theo Verweij
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
>
>
Author
25 Aug 2006 8:00 PM
SStory
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
Author
25 Aug 2006 8:24 PM
Kerry Moorman
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
>
>
>
Author
25 Aug 2006 9:26 PM
SStory
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
>>
>>
>>
Author
25 Aug 2006 9:15 PM
Theo Verweij
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
>
>
Author
26 Aug 2006 1:00 AM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"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
|
|
Author
28 Aug 2006 7:25 PM
SStory
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
> |
> |
>
>