Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: foreach loop error

Reply
Thread Tools

Re: foreach loop error

 
 
Kevin Spencer
Guest
Posts: n/a
 
      08-22-2003
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" <(E-Mail Removed)> wrote in message
news:061401c368f1$e58c9ab0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
How can I delete a Item in foreach loop ad ASP .Net 2 06-06-2005 01:15 AM
loop througth Literials using foreach!! Islam Elkhayat ASP .Net 16 02-12-2005 01:51 AM
Problem with foreach loop =?Utf-8?B?SnVzdGlu?= ASP .Net 2 10-22-2004 08:35 AM
Re: foreach loop error Luc Kumps ASP .Net 0 08-22-2003 10:16 PM



Advertisments