Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get key by idex from a ordered dictionary

Posted on 2006-10-24
8
Medium Priority
?
4,588 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?)
0
Comment
Question by:Kai77
[X]
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
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:jonorossi
ID: 17795792
Have you tried using the TryGetValue method?
0
 

Author Comment

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

Expert Comment

by:jonorossi
ID: 17795890
Is there a reason you are using System.Collections.Specialized.OrderedDictionary instead of System.Collections.Generic.Dictionary<TKey,TValue>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Kai77
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?
0
 

Author Comment

by:Kai77
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.
0
 
LVL 9

Expert Comment

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

System.Collections.Generic.SortedDictionary<TKey, TValue>
0
 
LVL 9

Expert Comment

by:jonorossi
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.
0
 
LVL 9

Accepted Solution

by:
jonorossi earned 1500 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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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