Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Get key by idex from a ordered dictionary

Posted on 2006-10-24
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?)
Question by:Kai77
  • 5
  • 3

Expert Comment

ID: 17795792
Have you tried using the TryGetValue method?

Author Comment

ID: 17795845
I am using ASP.net 2.0 and intellisense does not say OrderDictionary has a TryGetValue method

Expert Comment

ID: 17795890
Is there a reason you are using System.Collections.Specialized.OrderedDictionary instead of System.Collections.Generic.Dictionary<TKey,TValue>
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Author Comment

ID: 17795986
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 Comment

ID: 17796028
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.

Expert Comment

ID: 17796034
If you require that the dictionary be sorted then you should use the generic SortedDictionary:

System.Collections.Generic.SortedDictionary<TKey, TValue>

Expert Comment

ID: 17796213
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.

Accepted Solution

jonorossi earned 500 total points
ID: 17796543
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 28
Windows Media Player 2 31
C#.net split string and then check appropriate checkboxlist 4 26
C# Linq Select From List 3 10
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question