运行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]);
}
}
答案 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 : ";
}