0

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;
}
Community
  • 1
  • 1
Will
  • 1,206
  • 9
  • 22
  • Maybe I don't get it, but what's wrong with `var DefaultLists = new List(){ ScanDefaultTasks, BehDefaultTasks,MEGDefaultTasks,DefaultQuestionnaires };`? – Konrad Kokosa Jan 09 '14 at 21:54
  • @KonradKokosa like the CheckBoxList[] construction, the page hangs until timeout. Why, I don't understand yet. – Will Jan 09 '14 at 21:58

2 Answers2

1

DefaultLists is an array that stores references to the contained CheckBoxLists. So I do not see a problem there. The only problem I can spot in your sample is that you use i where you should use j in the inner loop:

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[j].Selected = true;
    }
}

I've changed the second i to a j in this line:

DefaultLists[i].Items[j].Selected = true;

In order to simplify things a bit, you could also use for each for the loops:

CheckBoxList[] DefaultLists = { ScanDefaultTasks, BehDefaultTasks,MEGDefaultTasks,DefaultQuestionnaires };
foreach(var cbl in DefaultLists)
    foreach(var item in cbl.Items)
        item.Selected = true;
Markus
  • 20,838
  • 4
  • 31
  • 55
  • also needed to change an i->j inside the for loop conditional. given that I can't distinguish between the indexes I choose, foreach seems like a much better solution! – Will Jan 09 '14 at 22:09
0

You could possibly do something like this:

foreach(var item in new CheckBoxList[]{
                             ScanDefaultTasks, 
                             BehDefaultTasks,
                             MEGDefaultTasks,
                             DefaultQuestionnaires}
                      .SelectMany(l => l.Items))
  item.Selected = true;
Nate
  • 7
  • 2