开发者

Dictionary lookup using a variable

开发者 https://www.devze.com 2023-03-26 11:15 出处:网络
Can a dictionary of type string and CheckBox be parsed by a variable string in such a way that should the variable string find a dictionary entry that matches it, i开发者_运维知识库t will set the corr

Can a dictionary of type string and CheckBox be parsed by a variable string in such a way that should the variable string find a dictionary entry that matches it, i开发者_运维知识库t will set the corresponding checkbox to true?


Yes, you can achieve that using following code. Let's say you have myDictionary<string, CheckBox> and a string stringToCheck which contains that value you want to find in the dictionary You can do something like this

string stringToCheck = "something";

if(myDictionary.ContainsKey(stringToCheck))
{
    myDictionary[stringToCheck].Checked = true;
}


Is Dictionary.ContainsValue what you are looking for?

http://msdn.microsoft.com/en-us/library/a63811ah.aspx


It seems like you're asking: I have a Dictionary. I want to set the corresponding checkbox to true for a given string. That can be accomplished by the following

Dictionary<string, CheckBox> dictionary = <elided>;
CheckBox checkBox = dictionary[valueToSearch];
checkBox.Checked = true;


I would use TryGetValue to reduce the accesses to the dictionary:

Dictionary<string, CheckBox> aDict;
// your code here
CheckBox tmp;
if (aDict.TryGetValue(stringToSearch, out tmp))
    tmp.Checked = true;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号