2017-09-04 113 views
1

我想扩大我的字典用法,并希望为我的对有多个值。C#元组词典

我对添加项目到字典没有任何问题。 我试图使用Tuple作为选项,但似乎无法弄清楚如何提取特定的值。

这个工程:

Dictionary<string, string> voc4 = new Dictionary<string, string>(); 
voc1.Add(key1, value); 

if (voc1.TryGetValue(result.Text.ToLower(), out string cmd)) 
{ 
    ToSend(cmd); 
} 

我试图建立与元组新的辞典:

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 
} 

我怎样才能得到一定的价值基础上的密钥,并用他们不同的功能? 例子是:

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

    ToDisplay(value 2 from tuple).ToString();  
} 

任何帮助将是巨大的

+0

不是没有,但你问了7个问题,并得到10个答案,但没有一个答案被接受。接受答案表示回答的问题(“已解决”)。随着投票,它还可以帮助其他用户找到好的答案/帖子。即使您无法发布答案,也可以帮助他人。 [旅游]用约2分钟的图片解释它 – Plutonix

+0

我很抱歉。我不知道该如何解决和帮助。 –

+0

而不是'out string cmd'(甚至编译?)它应该是'out Tuple tuple',因为这是'voc5'字典中的值类型。 – Dialecticus

回答

0

你只能得到整个Tuple<string, string>比如你的字典。即使你只想使用其中一个值,也必须将整个元组取出。

检索元组后,使用ItemN来访问元素。

if (voc5.TryGetValue(result.Text.ToLower(), out Tuple<string, string> cmd)) 
{ 
    ToSend(cmd.Item1).ToString(); 

    ToDisplay(cmd.Item2).ToString(); 
} 
+0

那是我失踪的部分。谢谢 –