1

I have a checkBox in my Windows application called "Continuous". When user checks this and clicks "Process" button, application will process all the items in the listBox. However if user does not check this box it will only process the first one in the list.

In my Process method I want to write an if condition to check the checkBox checked and execute the foreach loop otherwise execute just the first item.

Here is my code

private void btnProcess_Clicl()
{

  bool bDone = false;

  while(!bDone)
  {

    LoadList(); //This will load the list from database into listBox

    if(listBox.items.Count > 0)
    {
      ProcessList();
    }

    if(!chkBox.Checked)
      bDone = true;

  }

}

I've implement the foreach loop to process list in ProcessList() method. Is there anyway to avoid executing LoadList() method from executing if the user checks continuous checkBox? LoadList() will populate the listBox from database.

4
  • Is the issue that you only want to try to ProcessList() once if the checkbox is checked, or is it that you want to avoid loading the entire list from the db if it isn't checked? Commented Aug 4, 2010 at 17:46
  • Be careful... it sounds like you want to modify the list you are looping over. You can't do this inside of a foreach loop. Commented Aug 4, 2010 at 17:46
  • Why is your if(!chkBox.Checked) at the bottom? I don't really know what you are asking i'm afraid but without further explanation i'd guess you should be checking if that box is checked or not before doing anything. Commented Aug 4, 2010 at 17:48
  • Dan - I want to execute ProcessList() only once and after executing it once Iam checking if the user has clicked continuous or not. Depending on that I am exiting the while loop. Commented Aug 4, 2010 at 18:15

3 Answers 3

2

do something like this

if( chkBox.Checked )
    ProcessList();
else
    ProcessOne();

Write the functions to do what you want

update

to avoid duplicating the processing code you could do something like

public void ProcessList()
{
    foreach( var item in list )
        ProcessOne( item );
}
Sign up to request clarification or add additional context in comments.

3 Comments

So, this is like writing the code twice in ProcessList() and ProcessOne() method. Right?
I think this makes sense. I was thinking of a way not to write the code twice but I think there is no other way by looking at the answers. Thanks.
@shanthiram you don't necessarily need to duplicate code. see update incoming...
2

Factoring is your friend.

void ProcessList(int start, int count) {
    for (int i=start; i < start + count; i++) {
        ProcessItem(i);
    }
}

void ProcessItem(int i) { // your code here
}

private void btnProcess_Click() {
   if (IsContinuous) {
      ProcessList(0, list.Count);
   }
   else {
       ProcessItem(0);
   }
}

private bool IsContinuous { get { return chkBox.Checked; } }

This will work for you but I don't especially like it since I think Process should be part of the list data structure itself and not my UI. Model (and View) and Control should be separate (if possible).

Comments

1
    Boolean doAllItems = chkBox.Checked;
    foreach(Object something in collection)
    {
        DoWork(something);
        if(!doAllItems)
            break;
    }

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.