We help IT Professionals succeed at work.

Get key by idex from a ordered dictionary

Kai77
Kai77 asked
on
Medium Priority
5,048 Views
Last Modified: 2008-01-09
How can I retrieve the key by using the index?

I can retrieve the value by index like: oOrderedDictionary[0].toString()

Is there something similair for a key value?
(I believe a sortedlist has a GetKey() method, but the ordered dictionary does not?)
Comment
Watch Question

Have you tried using the TryGetValue method?

Author

Commented:
I am using ASP.net 2.0 and intellisense does not say OrderDictionary has a TryGetValue method
Is there a reason you are using System.Collections.Specialized.OrderedDictionary instead of System.Collections.Generic.Dictionary<TKey,TValue>

Author

Commented:
from what i undestand an ordered dictionary the sort order is ordered by the order in which you addes key-value pairs. Does the generic.dictionary do the same?

Author

Commented:
I just took a quick look at the Generic.Dictionary .

I believe the TryGetValue needs you to enter a key and will return a value based on that key. What I need is to enter a index and get the key.
If you require that the dictionary be sorted then you should use the generic SortedDictionary:

System.Collections.Generic.SortedDictionary<TKey, TValue>
I see what you are after now. I can't seem to find what you are after in any of the collections built into .NET. For some reason none of them allow you to get the key by an index.
The reason you can't get the key by index with the SortedDictionary is because it is implemented as a binary tree. However, I am not sure why Microsoft didn't add a method to get the Key by index with the OrderedDictionary. Even the OrderedDictionaryKeyValueCollection which you get by going .Keys doesn't have a method to get out a key.

I was thinking get out the OrderedDictionaryKeyValueCollection from .Keys and enumerate the number of times to get to the index. But the GetEnumerator is declared internal so you can't use a foreach.

The only solution I can see is to build your own dictionary either from scratch or inherited from one of the .NET ones. Or try to get one off the internet. Or if you don't need it to be ordered you'd be able to use one of the .NET collections.

This one by Marc Clifton is pretty good http://www.codeproject.com/csharp/GenericKeyedList.asp

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.