Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to remove items from an ArrayList 1

Status
Not open for further replies.

craigber

Programmer
May 5, 2003
1,092
US
I have an ArrayList (activeWatches) that holds references to an object (activeWatch). I need to remove items based on a condition in the object. The problem is, I don't know how to remove the item without getting an error. Here's the code I have now.

foreach (WatchItem activeWatch in activeWatches)
{
if (activeWatch.CanDelete())
activeWatches.Remove(activeWatch);
}

Because the number of activeWatches changes inside the loop, I get an error. Any ideas on how to do this?

Craig Berntson
MCSD, Visual FoxPro MVP, Author, CrysDev: A Developer's Guide to Integrating Crystal Reports"
 
Code:
WatchItem[] watches = (WatchItem[])activeWatches.ToArray(typeof(WatchItem));

for (int i = 0; i < watches.length; i++)
{
    if (watches[i].CanDelete())
    {
        activeWatches.Remove(watches[i]);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top