ForEach & Remove Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Here's some pseudo-code that describes what I'm trying to do:

foreach(object in collection)
{
if (certain-condition)
collection.Remove(object);
}

The problem with this is that foreach gets messed up if anything is deleted
from the collection. What's the best way to handle such a situation?
 
In VB 2005, use

for i as Integer in collection.count -1 to 0 step -1
if collection(i) = condition then collection.removeat(i)
next i

Iterators don't like removing objects.

Mike Ober.
 
Robert,

It doesn't matter, this has been a limitation in C# from version 1.0
(for good reason).
 
I don't know if this is the optimum solution, but here's what I've come up:

int i = 0;
do
{
ObjectClass object = collection;
if (test == false)
i++;
else
collection.Remove(object);
} while (i < collection.Count);




--
Robert W.
Vancouver, BC
www.mwtech.com



Nicholas Paldino said:
Robert,

It doesn't matter, this has been a limitation in C# from version 1.0
(for good reason).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert W. said:
And in C# 2003 ?
 
Robert said:
I don't know if this is the optimum solution, but here's what I've come up:

int i = 0;
do
{
ObjectClass object = collection;
if (test == false)
i++;
else
collection.Remove(object);
} while (i < collection.Count);

Try a reverse for loop;

for(int i = col.Count -1; i >= 0; i--)
{
if(..test..)
{
col.Remove(i);
}
}
 
Hi Robert,

I solved it like this:

SortedList temp=new SortedList(clientsUse);
foreach (object key in temp.Keys)
{
if (key.Equals(lbClients.SelectedItem)) clientsUse.Remove(key);
}

So make a copy and remove the stuff from the original list You have copied from!

Problem solved?

Chris
 
You can use for statement both in VS 2005 or 2003:

for( int i=collection.Count;i>=0;i--)
{
//...
}
 
Robert said:
Here's some pseudo-code that describes what I'm trying to do:

foreach(object in collection)
{
if (certain-condition)
collection.Remove(object);
}

The problem with this is that foreach gets messed up if anything is deleted
from the collection. What's the best way to handle such a situation?

The solutions offered you that rely on iterating by an index might well
work, but make me think 'yuck' when I read them. I would prefer:

// terrible pseudocode follows
// keepthese will hold the objects we want to keep
collection keepthese = new collection
foreach object in originalcollection
// if not removing this object, then keep it
if (!removalcondition)
keepthese.add(object);

// now we have just those objects we want to keep
// so keep them
originalcollection = keepthese;


For anyone worried about the cost of this, remember there is no actual
copying of objects here, just copying of references, which is cheap.
 
Hi,

I also prefer a reverse loop, it does not affect the index.

Another possible solution that can be used in a collection that cannot be
used with index is using a temporary list to hold the values being deleted:

ArrayList ar = new ArrayList();

foreach(object o in collection)
{
if (certain-condition)
ar.Add(o);
}

foreach(object o in ar)
coollection.Remove( o);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top