473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# 2.0: Generic Dictionary - How to lookup a Value and return the key ?

Hi There

I am probably missing something fundamental here, but I cannot see a
method to search the values of a generic dictionary so that I can find
the key ? Of course I could enumerate through the Values collection
but that seems a little long winded.

I ended up using a sorted list because it has the IndexOfValue method.
However I don't need the sorting.

What am I missing ? fyi, the class is below

TIA
Bill

_____________________
public abstract class AusStates

{

static SortedList<char, string> stateListing = new SortedList<char,
string>();

public AusStates()

{

stateListing.Add('2', "NSW");

stateListing.Add('3', "VIC");

stateListing.Add('4', "QLD");

stateListing.Add('5', "SA");

stateListing.Add('6', "WA");

stateListing.Add('7', "TAS");

stateListing.Add('A', "ACT");

stateListing.Add('0', "NT");

}

public static char GetStateChar(string itemToLookup)

{

int indexToFind =
stateListing.IndexOfValue(itemToLookup.Trim().ToUp per(CultureInfo.CurrentCulture));

if (indexToFind == -1)

return ' '; //Keep consistent with old design

else

return stateListing.Keys[indexToFind];

}

public static string GetStateTxt(char itemToLookup)

{

int indexToFind = stateListing.IndexOfKey(itemToLookup);

if (indexToFind == -1)

return " "; //Keep consistent with old design

else

return stateListing.Values[indexToFind];

}

}

Nov 17 '05 #1
2 31639
On 4 Aug 2005 21:30:09 -0700, or*******@yahoo.com.au wrote:
I am probably missing something fundamental here, but I cannot see a
method to search the values of a generic dictionary so that I can find
the key ? Of course I could enumerate through the Values collection
but that seems a little long winded.


You're not missing anything, and linear enumeration is indeed the only
way to find a value in a hashtable (which is what a Dictionary is).

Enumerating through the Values collection alone still won't help you
find the associated key, by the way -- you'll have to iterate through
the Dictionary itself:

TKey GetByValue(IDictionary<TKey, TValue> dictionary, TValue value) {
if (dictionary == null)
throw new ArgumentNullException("dictionary");

foreach (KeyValuePair<TKey, TValue> pair in dictionary)
if (value.Equals(pair.Value)) return pair.Key;

return default(TKey); // or throw exception
}

This will crash if value is null, by the way. You can't directly test
value for null in the July CTP due to a compiler bug. If you want to
allow null values you'll have to do a more elaborate value comparison.
--
http://www.kynosarges.de
Nov 17 '05 #2
>What am I missing ? fyi, the class is below

That multiple keys may have the same value? The method you suggest
would have to return all keys that have the given value (or perhaps
the first one found, but since a dictionary isn't ordered that may be
unpredictable).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: John | last post by:
I am trying to design a dictionary in such a way that I can keep the interface and implementation separate. Here is my initial design. Anyone has any comments on it or suggestions on how I could...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
18
by: Rune B | last post by:
Hi Group I was considering using a Generic Dictionary<> as a value container inside my business objects, for the reason of keeping track of fields changed or added and so on. - But how...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
4
by: Ram | last post by:
Hi All, I am new to this Generics. I started with a Generic method which doesn't return any value. It worked well. Then I tried to write a generic method which will sum up the given values...
2
by: Stephen Costanzo | last post by:
I have: Public Class test Private displayValue As String Private dbType As Integer Property Display() As String Get Return displayValue End Get
3
by: Phil Sandler | last post by:
All, I have a situation where I need a List<stringthat performs like a generic dictionary (Dictionary<string, string>). Essentially, I just need the key, not the value, so I want to use as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.