By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

ForEach & Remove Problem

P: n/a
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?

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

Jul 6 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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 W." <Ro*****@discussions.microsoft.comwrote in message
news:96**********************************@microsof t.com...
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?

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



Jul 6 '06 #2

P: n/a
And in C# 2003 ?

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

"Michael D. Ober" wrote:
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 W." <Ro*****@discussions.microsoft.comwrote in message
news:96**********************************@microsof t.com...
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?

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

Jul 6 '06 #3

P: n/a
"Robert W." <Ro*****@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
>for i as Integer in collection.count -1 to 0 step -1
if collection(i) = condition then collection.removeat(i)
next i
And in C# 2003 ?
Oh, c'mon. At least make an -effort-.

///ark
Jul 6 '06 #4

P: n/a
Robert,

It doesn't matter, this has been a limitation in C# from version 1.0
(for good reason).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Robert W." <Ro*****@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
And in C# 2003 ?

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

"Michael D. Ober" wrote:
>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 W." <Ro*****@discussions.microsoft.comwrote in message
news:96**********************************@microso ft.com...
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?

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



Jul 6 '06 #5

P: n/a
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[i];
if (test == false)
i++;
else
collection.Remove(object);
} while (i < collection.Count);


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

"Nicholas Paldino [.NET/C# MVP]" wrote:
Robert,

It doesn't matter, this has been a limitation in C# from version 1.0
(for good reason).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Robert W." <Ro*****@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
And in C# 2003 ?

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

"Michael D. Ober" wrote:
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 W." <Ro*****@discussions.microsoft.comwrote in message
news:96**********************************@microsof t.com...
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?

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





Jul 6 '06 #6

P: n/a
Robert W. wrote:
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[i];
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);
}
}
Jul 6 '06 #7

P: n/a
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
"Robert W." <Ro*****@discussions.microsoft.comschrieb im Newsbeitrag news:96**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com
Jul 6 '06 #8

P: n/a
You can use for statement both in VS 2005 or 2003:

for( int i=collection.Count;i>=0;i--)
{
//...
}

Robert W. wrote:
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?

--
Robert W.
Vancouver, BC
www.mwtech.com
Jul 6 '06 #9

P: n/a

Robert W. wrote:
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.

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 6 '06 #10

P: n/a
Hi Robert,
It might be simplest for you just to iterate backwards through the
collection, but Eric Gunnerson had a good article on an "IterIsolate"
utility class that handles cases like this. You might want to check
out his MSDN article:

http://msdn.microsoft.com/library/de...rp01212002.asp

John

Jul 6 '06 #11

P: n/a
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);
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
>>
Try a reverse for loop;

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

Jul 6 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.