Re: foreach loop error

Discussion in 'ASP .Net' started by Kevin Spencer, Aug 22, 2003.

  1. Use an integer counter, start from the end of the Collection, and work your
    way back to the beginning. Something like (not checked for typos):

    for (int intCt = mymListBox.Items.Count - 1; intCt > -1; intCt--)
    {
    if ((Convert.ToString(mymListBox.Items[intCt].Value)).StartsWith(string))
    chosenPractices.Items.Remove(mymListBox.Items[intCt]);
    }

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    The more I learn, the less I know.

    "Will" <> wrote in message
    news:061401c368f1$e58c9ab0$...
    > I'm trying to run through all of the listitems in a
    > listbox with a foreach loop. it looks like this:
    >
    >
    > foreach (ListItem temp in mymListBox.Items) {
    > if ((temp.ToString()).StartsWith(string)) {
    > chosenPractices.Items.Remove(temp);
    > }
    > }
    >
    > I get the following error on the very first line:
    >
    > System.InvalidOperationException: Collection was modified;
    > enumeration operation may not execute.
    >
    > please help!
    >
    >
    > Will
    Kevin Spencer, Aug 22, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    Luc Kumps
    Aug 22, 2003
  2. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    319
    Hans Kesting
    Oct 22, 2004
  3. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    739
    Scott Allen
    Feb 12, 2005
  4. ad
    Replies:
    2
    Views:
    14,615
    Peter Rilling
    Jun 6, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page