?
Solved

Removing elements in collections

Posted on 2005-05-05
2
Medium Priority
?
228 Views
Last Modified: 2010-04-16
it seems like collections like array list and hashtables dont allow removing elements inside a loop ?

Am I correct ?
0
Comment
Question by:deersuper
2 Comments
 
LVL 1

Accepted Solution

by:
vbguest earned 150 total points
ID: 13939078
Yes. You're correct. You get an "System.InvalidOperationException: Collection was modified; enumeration operation may not execute." exception if you try to do it.

To get around this I make a copy of the collection and remove the items from the copy as I'm looping through the original collection.
0
 
LVL 7

Expert Comment

by:jj819430
ID: 13939306
during a foreach loop you can't do it, but you can through a standard while or for loop.
for(int i = 0; i < ArrayList.count;i++)
{
  if(Delete ArraList[i])
  {
  ArrayList.RemoveAt(i);
  i--;
  }
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question