简体   繁体   中英

C# String Array Returns Wrong Values

When i run my program i get wrong values from string array and i have no idea why this is happening...

My code :

BackGroundWorker bgw = new BackGroundWorker();
Form1_Load(object sender, EventArgs e)
{
    bgw.DoWork += delegate {
        Open();
    }
    bgw.RunWorkerAsync();
}
private void Open()
{
    string line = "put --------------------,true,10,0";

    var lineContent = line.Split(' ');
    var syx = lineContent[0];
    var cont = lineContent[1];
    var contents = cont.Split(',');

    if (syx == "put")
    {
        if (contents.Length == 4) // In Debug This Is True
        {
            // Debugger : contents[0] = "-------------------"
            //                    [1] = "true"
            //                    [2] = "10"
            //                    [3] = "0"
            string m = contents[0]; // m = " "
            string r = contents[1]; // r = "true"
            string s = contents[2]; // s = "1"
            string rnd = contents[3]; // rnd = "0"
        }
    }
}

As you can see , my contents array have the correct values but when i try to get values form my array , it gives me wrong or incomplete values调试器 Is my code wrong ? or something else ?

Check this:

string[] lineContent = new string[] { };
string syx = "";
string cont = "";
string[] contents = new string[] { };
string er = "";

string line = "put --------------------,true,10,10";
lineContent = line.Split(' ');
syx = lineContent[0];
cont = lineContent[1];
contents = cont.Split(',');

if (syx == "put")
{
  if (contents.Length == 4) // In Debug This Is True
  {
    // Debugger : contents[0] = "-------------------"
    //                    [1] = "true"
    //                    [2] = "10"
    //                    [3] = "10"
    string m = contents[0]; // m = " "
    string r = contents[1]; // r = "true"
    string s = contents[2]; // s = "1"
    string rnd = contents[3]; // rnd = "0"
  }

}

通过使用不同的变量名称修复......不知道为什么,但这样我的问题就解决了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM