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

O

orekinbck

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().ToUpper(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];

}

}
 
C

Christoph Nahr

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.
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top