C# 代码在if语句中不工作

C# 代码在if语句中不工作,c#,if-statement,C#,If Statement,我在考虑下面代码中的if/else语句时遇到了问题。实际上,我想告诉我的用户,如果找不到文件中的单词,我的代码必须显示错误消息,否则我的代码必须显示新表单,以下是我的代码: public void searchGlossary(String word) { StringBuilder description = new StringBuilder(512); string descLine; using (StreamReader

我在考虑下面代码中的if/else语句时遇到了问题。实际上,我想告诉我的用户,如果找不到文件中的单词,我的代码必须显示错误消息,否则我的代码必须显示新表单,以下是我的代码:

    public void searchGlossary(String word)
    {
        StringBuilder description = new StringBuilder(512);
        string descLine;
        using (StreamReader reader = File.OpenText("C:/Users/--- /Desktop/--- .txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith(word, StringComparison.OrdinalIgnoreCase))
                {
                    // At this point we've already read the keyword and it matches our input

                    // Here we start reading description lines after the keyword.
                    // Because every keyword with description is separated by blank line
                    // we continue reading the file until, the last read line is empty 
                    // (separator between keywords) or its null (eof)
                    while ((descLine = reader.ReadLine()) != string.Empty && descLine != null)
                    {
                        description.AppendLine(descLine);
                    }
                    //descbox.Text = description.ToString();
                    isfound = true;
                    DomainExpertForm form = new DomainExpertForm(keyword, description.ToString());
                    form.Show();
                    break;
                }

            }

            if (isfound == false)
            {
                MessageBox.Show("No Matches found for Word " + word, "Domain Expert Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


    }

问题是,如果找不到单词,它总是显示表单,而不是显示错误消息。有什么问题和解决办法有人能帮我吗我是新来的c#

您应该在方法的开头用
false
声明并初始化
isFound

public void searchGlossary(String word)
{
  var isFound = false; 
  StringBuilder description = new StringBuilder(512);

  ...
}

您应该在方法的开头声明并初始化
isFound
,使用
false

public void searchGlossary(String word)
{
  var isFound = false; 
  StringBuilder description = new StringBuilder(512);

  ...
}

我需要的是检查是否找到了所需的单词,如果没有,我想显示错误对话框,因此以下是我的解决方案:

    I simply checked the length of the found string; if its ==0 i show up an error message as:

        if (description.ToString().Length==0)
        {
            MessageBox.Show("No Matches found for Word " + word, "Domain Expert Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

我自己找到了这个解决方案

我需要的是检查是否找到了所需的单词,如果没有,我想显示错误对话框,因此以下是我的解决方案:

    I simply checked the length of the found string; if its ==0 i show up an error message as:

        if (description.ToString().Length==0)
        {
            MessageBox.Show("No Matches found for Word " + word, "Domain Expert Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

我自己找到了这个解决方案

在StringBuilder之前将isFound初始化为false(bool isFound=false)。在哪里定义了“isFound”?尝试在方法的开头定义它并用false初始化。顺便说一句,您不需要在if语句中测试布尔变量,您只需在if(!isfound)中执行此操作即可。我已尝试过,但没有成功。您是否尝试在文件的一行中查找
word
?或者整个文件?在StringBuilder之前将isFound初始化为false(bool isFound=false)。在哪里定义了“isFound”?尝试在方法的开头定义它并用false初始化。顺便说一句,您不需要在if语句中测试布尔变量,您只需在if(!isfound)中执行此操作即可。我已尝试过,但没有成功。您是否尝试在文件的一行中查找
word
?或者整个文件?不仅如此,你还应该在那里声明。在更高的范围内声明它是没有意义的。不仅如此,你还应该在那里声明它。在更高的范围内声明它是没有意义的。