-4

我对 C# 还很陌生,对某些事情感到困惑.....

让我告诉你发生了什么,希望你们能告诉我我在这里做错了什么。

string incomming = Encoding.UTF8.GetString(bytes);
//MessageBox.Show(incomming); shows me the string "stop", No problem

executeCommand(incomming);

public void executeCommand(string action)
{
    MessageBox.Show(action + " was recieved"); // shows the string "stop", No  problem here... that works

    switch (action)
    { 
        case "start":
            MessageBox.Show("start was recieved"); //shows nothing
            break;

        case "stop":
             MessageBox.Show("stop was recieved"); //shows nothing
             break;
    }            
}
4

2 回答 2

1

在不知道要转换为字符串的 Byte 数组的内容的情况下,很难为您提供任何帮助。但这里有几件事可以尝试。

  1. executeCommand(incomming)您可以在监视窗口类型中放置断点incomming.ToCharArray(),您需要单击值列中的绿色圆圈,然后才能看到字符串之外的字符。这应该让你知道你正在处理什么。

  2. 您可以使用String.Contains方法在传入的字符串中搜索您要查找的字符串的匹配项。


if (action.Contains("stop"))
    MessageBox.Show("stop was recieved");
else if (action.Contains("start"))
    MessageBox.Show("start was recieved");
于 2012-04-26T06:54:58.863 回答
0

对此唯一合理的解释是,您的“停止”字符串中有一个字母与英语不同,但看起来相同。在这种情况下:

"stop" != "stоp"  

是真的;

于 2012-04-26T04:59:36.037 回答