Solved

Get key by idex from a ordered dictionary

Posted on 2006-10-24
8
4,457 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
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:jonorossi
Comment Utility
Have you tried using the TryGetValue method?
0
 

Author Comment

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

Expert Comment

by:jonorossi
Comment Utility
Is there a reason you are using System.Collections.Specialized.OrderedDictionary instead of System.Collections.Generic.Dictionary<TKey,TValue>
0
 

Author Comment

by:Kai77
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Kai77
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now