On Thursday, October 22, 2009 3:46 PM namdia wrote:
public int findMyValue(Dictionary<int,strnig> myDic, string val)
{
foreach(int key in myDic.Keys)
{
if ( myDic[key ] == val)
{
return key ;
}
}
return null;
}
ciupazNoSpamGrazi wrote:
Get Dictionary key by value
22-Sep-08
Hi all,
having a dictionary of <int, string> (C# 2.0) is there a way to retrieve the
key for a specific value (of type string)?
Obviously I populate the Dictionary with not duplicated strings.
Thanks
Previous Posts In This Thread:
ciupazNoSpamGrazi wrote:
Get Dictionary key by value
Hi all,
having a dictionary of <int, string> (C# 2.0) is there a way to retrieve the
key for a specific value (of type string)?
Obviously I populate the Dictionary with not duplicated strings.
Thanks
Mark Rae [MVP] wrote:
Re: Get Dictionary key by value
http://www.google.co.uk/search?hl=e...252&q="C#"+Dictionary+find+key+by+value&meta=
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Jon Skeet [C# MVP] wrote:
Re: Get Dictionary key by value
No - a dictionary is a one way mapping. If you want to be able to map
both ways, you need two dictionaries.
--
Web site:
http://www.pobox.com/~skeet
Blog:
http://www.msmvps.com/jon.skeet
C# in Depth:
OD wrote:
dictionnaries are "one way".
dictionnaries are "one way". you will have to iterate or use a second
dictionnary.
You said you are using C# 2.0, so just to show you what C#3.0 can bring,
you can simplify your code using a Linq que
Marc Gravell wrote:
Probably the easiest option here is to loop over the pairs; public
Probably the easiest option here is to loop over the pairs;
public static int? FindKey(
this IDictionary<int, string> lookup,
string value)
{
foreach (var pair in lookup)
{
if (pair.Value == value)
r
Tom Jackson wrote:
Get dictionary key by value
Assuming you have a "deletedMsgs" dictionary and you want to remove a message from the "failedPosts" queue, but you need the failedPosts key that matches the value found in deletedMsgs....
int keyFou
EggHeadCafe - Software Developer Portal of Choice
Programming C#
http://www.eggheadcafe.com/tutorials/aspnet/718b8580-cd75-4234-8764-db65bebbe8ad/programming-c.aspx