在功能中检测到无法访问的代码

时间:2013-10-04 09:24:42

标签: c# winforms visual-studio

运行VS时出现“无法访问代码检测”错误。我在这两行中得到了错误:

MoveListItems(listBox1,listBox2)ReplaceListItems(listBox1,listBox2)

感谢任何帮助!

以下代码:

    private string CreateNewEntry(string current)
    {
        var indexIn = current.LastIndexOf("Time In : "); // Get the last index of the word "in"
        var indexOut = current.LastIndexOf("Time Out : "); // Get the last index of the word out

        if (indexOut > indexIn)
        {
            return current + "      "+"Time In : "; // if the last "out" comes after the last "in"
            ReplaceListBoxItems(listBox1,listBox2);
        }
        else
        {
            // If the last "in" comes after the last "out"
            return current + "      " +"Time Out : ";
            MoveListBoxItems(listBox1,listBox2);
        }
    }

    private void MoveListBoxItems(ListBox source, ListBox destination)
    {
        ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
        foreach (var item in sourceItems)
        {
            destination.Items.Add(item);
        }
        while (source.SelectedItems.Count > 0)
        {
            source.Items.Remove(source.SelectedItems[0]);
        }
    }

3 个答案:

答案 0 :(得分:4)

你有return之后的代码 - 陈述。永远不会达到该代码,因为返回退出方法。

考虑将ReplaceListBoxItems(listBox1,listBox2);移到return之上。

答案 1 :(得分:3)

这是因为您在这些行之前使用return关键字。这种风格可能会有所帮助

ReplaceListBoxItems(listBox1,listBox2);
return current + "      "+"Time In : "; // if the last "out" comes after the last "in"

答案 2 :(得分:1)

只需替换以下行而不是

         if (indexOut > indexIn)
          {
            ReplaceListBoxItems(listBox1,listBox2);
            return current + "      "+"Time In : ";// if the last "out" comes after the last "in"
          }
          else
          {
                // If the last "in" comes after the last "out"
                MoveListBoxItems(listBox1,listBox2);
                return current + "      " +"Time Out : ";
          }