1

I am trying to expand my usage of the dictionary and want to have multiple values for my pairs.

I have had no issues with adding items to the dictionary with one . I have tried to use Tuple as an option but cant seem to figure out how to extract a particular value.

this works:

 Dictionary<string, string> voc4 = new Dictionary<string, string>(); voc1.Add(key1, value); if (voc1.TryGetValue(result.Text.ToLower(), out string cmd)) { ToSend(cmd); } 

I tried to build a new dictionary with Tuple:

Dictionary<string, Tuple<string ,string>> voc5 = new Dictionary<string,Tuple<string ,string>>(); voc5.Add(key1, new Tuple<string, string>(value,response)); if (voc5.TryGetValue(result.Text.ToLower(), out string cmd)) {//this is what I cant get working. I want to get the first value of thedictionary for 1 purpose and the other for a different purpose } 

How can I get certain values based on the key and use them for different functions? example would be:

 if (voc5.TryGetValue(result.Text.ToLower(), out string cmd))// the first value { ToSend(value 1 from tuple).ToString(); ToDisplay(value 2 from tuple).ToString(); } 

Any help would be great

4
  • Not for nothing, but you have asked 7 questions and gotten 10 answers, but none of those answers are accepted. Accepting answers marks the question as answered ("RESOLVED"). Along with voting, it also helps other users find good answers/posts. Its a way you can help others even if you are not in a position to post answers. The tour explains it with pictures in about 2 mins Commented Sep 4, 2017 at 21:40
  • I apologize. I did not know to mark as resolved and helpful. Commented Sep 4, 2017 at 21:41
  • Instead of out string cmd (does that even compile?) it should be out Tuple<string, string> tuple, because that is the value type in voc5 dictionary. Commented Sep 4, 2017 at 21:41
  • no it does not. That was the problem. I was unsure how to build the last part. Commented Sep 4, 2017 at 21:47

1 Answer 1

1

You can only get the entire Tuple<string, string> instance out of your dictionary. Even if you'd only like to use one of the values, you have to fetch the entire tuple out.

After you retrieve the tuple use ItemN to access the elements.

if (voc5.TryGetValue(result.Text.ToLower(), out Tuple<string, string> cmd)) { ToSend(cmd.Item1).ToString(); ToDisplay(cmd.Item2).ToString(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.