0

运行 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]);
        }
    }
4

3 回答 3

4

您在 - 语句之后有代码return。永远不会到达该代码,因为 return 退出该方法。

考虑移到ReplaceListBoxItems(listBox1,listBox2);上面return

于 2013-10-04T09:25:28.627 回答
3

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

ReplaceListBoxItems(listBox1,listBox2);
return current + "      "+"Time In : "; // if the last "out" comes after the last "in"
于 2013-10-04T09:25:45.217 回答
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 : ";
          }
于 2013-10-04T09:33:30.263 回答