I've inherited some code I'd like to DRY up into to 2 loops.
The outer loop iterates over checkBoxeList object. The inner loop checks all the boxes.
CheckBoxList[] DefaultLists = { ScanDefaultTasks, BehDefaultTasks,MEGDefaultTasks,DefaultQuestionnaires };
for (int i = 0; i < DefaultLists.Count (); i++) {
for (int j = 0; i < DefaultLists[i].Items.Count; j++)
{
DefaultLists[i].Items[i].Selected = true;
}
}
How could DefaultList
be built such that it stores references to each of the checkBoxList objects?
How should these references be called?
It looks like one solution is to build a "box" class. This pattern occurs a few times, in most places duplicating greater lengths of redundant code. Building a class for each instance seems like overkill.
The original looks like
for (int i = 0; i < ScanDefaultTasks.Items.Count; i++)
{
ScanDefaultTasks.Items[i].Selected = true;
}
for (int i = 0; i < BehDefaultTasks.Items.Count; i++)
{
BehDefaultTasks.Items[i].Selected = true;
}
for (int i = 0; i < MEGDefaultTasks.Items.Count; i++)
{
MEGDefaultTasks.Items[i].Selected = true;
}
for (int i = 0; i < DefaultQuestionnaires.Items.Count; i++)
{
DefaultQuestionnaires.Items[i].Selected = true;
}