解析字符串逻辑问题c# [英] Parsing string logic issue c#

查看:131
本文介绍了解析字符串逻辑问题c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从这个问题的关注

我的程序正在接收一个由两部分组成的字符串:一个距离值和一个编号。我已经将它们分开并将它们存储在我的程序中的局部变量中。所有的id号码都存储在字典中,用于检查输入的距离值。虽然我应该注意到,从设备发送到我的程序中的每个字符串都被传递到单个字符串上。下一次我的程序接收到来自设备的信号时,它将覆盖之前的以前的数据。

My program is taking in a string that is comprised of two parts: a distance value and an id number respectively. I've split these up and stored them in local variables inside my program. All of the id numbers are stored in a dictionary and are used check the incoming distance value. Though I should note that each string that gets sent into my program from the device is passed along on a single string. The next time my program receives that a signal from a device, it overrides the previous data that was there before.

如果进入我的程序的id键与我的字典中的一个匹配,那么我的字典键旁边的一个变量应该被更新。然而,当我运行我的程序时,我没有得到6个不同的值,我只得到相同的值,它们都同时进行更新。

Should the id key coming into my program match one inside my dictionary, then a variable held next to my dictionaries key, should be updated. However, when I run my program, I don't get 6 different values, I only get the same value and they all update at the same time.

这是我写的所有尝试这样做的代码:

This is all the code I have written trying to do this:

    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(messages[1]))
    {
        Value1 = messageCount;
        Value2 = messageCount;
        Value3 = messageCount;
        Value4 = messageCount;
        Value5 = messageCount;
        Value6 = messageCount;
    }
}

我如何得到它,以便程序读取第一个键,例如1111111,它只更新Value1?

How can I get it so that when programs recives the first key, for example 1111111, it only updates Value1?

通过的信息可以是动态的,所以我想避免尽可能多的信息,因为我可能可以。

The information that comes through can be dynamic, so I'd like to avoid harding as much information as I possibly can.

推荐答案

而不是

if(myDictonary.ContainsKey(messages[1]))
{
  Value1 = messageCount;
  Value2 = messageCount;
  Value3 = messageCount;
  Value4 = messageCount;
  Value5 = messageCount;
  Value6 = messageCount;
}

使用

if(myDictonary.ContainsKey(messages[1]))
{
  myDictonary[messages[1]] = messageCount;
}

这样,您只需使用键$ $ c $更新字典条目c>消息[1] 而不是全部。

This way, you only update the dictionary entry with the key messages[1] and not all of them.

这篇关于解析字符串逻辑问题c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆