문제 설명
잘못된 값을 얻는 목록 확인 후 (After list checking getting wrong value)
그래서 목록을 만들고 요소를 추가했는데 나중에 읽고 싶은데 제대로 작동하지 않습니다. 제 경우에는 title이 true이고 checked.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
아이디어가 있습니까?
참조 솔루션
방법 1:
You can change it to switch case to make it look better and more manageable.
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;
}
}
And remember, you are running it in a loop. Your checkbox might be overwritten if you get two part.Title == "some chapter number"