所以我创建了列表并添加了元素,后来我想读它们。它并没有正常工作。在我的情况下,如果标题为true并且checkbox.checked为true,则应返回true值。但它给了我假。这是我的代码:
foreach (var part in config.Parts)
{
if (part.Title == "chapter2")
{ //true
checkBox1.Checked = part.Checked;
} //true
if (part.Title == "chapter3")
{
checkBox2.Checked = part.Checked;
}
} //false
有什么想法吗?
答案 0 :(得分:0)
您可以将其更改为切换案例,使其看起来更好,更易于管理。
foreach (var part in config.Parts)
{
switch(part.Title){
case "chapter2" :
checkBox1.Checked = part.Checked;
break;
case "chapter3" :
checkBox2.Checked = part.Checked;
break;
default :
throw new Exception("Case not handled");
break;
}
}
请记住,你是在循环中运行它。如果您获得两个part.Title == "some chapter number"