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

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.