动态更新数据c#中解析的字典

时间:2013-11-05 15:17:53

标签: c# string dictionary

我创建了一个包含已知键值的字典。

我还有一个字符串消息被解析到我的程序中,我将其拆分并存储在两个不同的变量中,这是在我的AppendString方法中完成的。这个字符串消息的一部分包含键值和我拆分的消息的另一部分我想用它来更新我的字典中保存的存储值,如果键匹配它。

但是,我想要更新的值不会更新。有人可以看看,告诉我我做错了什么?

Dictionary<string, string> myDictonary = new Dictionary<string, string>();
string Value1 = "";
string Value2 = "";
string Value3 = "";
string Value4 = "";
string Value5 = "";
string Value6 = "";

void Start() 
{

    myDictonary.Add("11111111", Value1);
    myDictonary.Add("22222222", Value2);
    myDictonary.Add("33333333", Value3);
    myDictonary.Add("44444444", Value4);
    myDictonary.Add("55555555", Value5);
    myDictonary.Add("66666666", Value6);

}

private void AppendString(string message) 
{   
    testMessage = message;

    string[] messages = message.Split(',');

    foreach(string w in messages)
    {   
        if(!message.StartsWith(" "))
            outputContent.text +=  w + "\n";

    }

    messageCount = "RSSI number " + messages[0];
    uuidString = "UUID number " + messages[1];



    if(myDictonary.ContainsKey(uuidString))
    {
        Value1 = messageCount;
        Value2 = messageCount;
        Value3 = messageCount;
        Value4 = messageCount;
        Value5 = messageCount;
        Value6 = messageCount;
    }
}

1 个答案:

答案 0 :(得分:1)

您的所有词典键都不以“UUID number”开头,因此检查myDictionary.ContainsKey(uuidString)时应始终返回false。尝试更改此内容:

if(myDictionary.ContainsKey(uuidString))

到此:

if(myDictionary.ContainsKey(messages[1])