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

Collection remove

P: n/a
Hi, I'm trying to remove items from a collection this way:

foreach(Object myO in ObjectCol){
if(myO != "xxx"){
myO.Remove();
}
}

But I'm having an error telling me that Collection was modified... I
understand this, since I remove one object from collection...

What is the best way to remove objects from my collection ?

TIA
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Franck Diastein wrote:
Hi, I'm trying to remove items from a collection this way:

foreach(Object myO in ObjectCol){
if(myO != "xxx"){
myO.Remove();
}
}

But I'm having an error telling me that Collection was modified... I
understand this, since I remove one object from collection...

What is the best way to remove objects from my collection ?

TIA

What exactly class you are using? There's no ObjectCol class in .NET,at least i didn't find one.
If you are using like ArrayList, then to remove object from collection just do:

foreach(Object o in al){
if(o != "xxx"){
al.Remove(o);
}
}

where al is your ArrayList object.

Hope it helps
Andrey
Nov 16 '05 #2

P: n/a
You cannot remove while in a foreach. Use for instead. Foreach gets the
collection enumeration and relies on that enumeration for looping, but when
you delete from the collection, the foreach gets confused. So, the solution
is to use for() instead

JIM

"Franck Diastein" <fd*******@euskaltel.net> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
Hi, I'm trying to remove items from a collection this way:

foreach(Object myO in ObjectCol){
if(myO != "xxx"){
myO.Remove();
}
}

But I'm having an error telling me that Collection was modified... I
understand this, since I remove one object from collection...

What is the best way to remove objects from my collection ?

TIA

Nov 16 '05 #3

P: n/a
I'm using a dock manager http://sourceforge.net/projects/dockpanelsuite/

There's a collection with all docks, but that collection has not Remove...

Thank you

MuZZy wrote:
Franck Diastein wrote:
Hi, I'm trying to remove items from a collection this way:

foreach(Object myO in ObjectCol){
if(myO != "xxx"){
myO.Remove();
}
}

But I'm having an error telling me that Collection was modified... I
understand this, since I remove one object from collection...

What is the best way to remove objects from my collection ?

TIA


What exactly class you are using? There's no ObjectCol class in .NET,at
least i didn't find one.
If you are using like ArrayList, then to remove object from collection
just do:

foreach(Object o in al){
if(o != "xxx"){
al.Remove(o);
}
}

where al is your ArrayList object.

Hope it helps
Andrey

Nov 16 '05 #4

P: n/a
MuZZy <le*******@yahoo.com> wrote:
Franck Diastein wrote:
Hi, I'm trying to remove items from a collection this way:

foreach(Object myO in ObjectCol){
if(myO != "xxx"){
myO.Remove();
}
}

But I'm having an error telling me that Collection was modified... I
understand this, since I remove one object from collection...

What is the best way to remove objects from my collection ?

TIA

What exactly class you are using? There's no ObjectCol class in
.NET,at least i didn't find one. If you are using like ArrayList,
then to remove object from collection just do:

foreach(Object o in al){
if(o != "xxx"){
al.Remove(o);
}
}

where al is your ArrayList object.


No, that's exactly the problem - that will throw an exception.

One way is to create a list of all objects you want to delete, then
remove then:

ArrayList removals = new ArrayList();
foreach (Object o in originalList)
{
if (o != "xxx")
{
removals.Add (o);
}
}

foreach (Object o in removals)
{
originalList.Remove(o);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
Don't forget that if you choose to use for instead of foreach, you must
loop _backward_ through the collection:

for (int i = myCollection.Length - 1; i >= 0; i--)
{
MyItemType item = (MyItemType)myCollection.Items[i];
if (item.Property == "xxx")
{
myCollection.Remove(item);
}
}

If you loop forward through the collection, then removing an item will
change the indices of all succeeding items, and you will end up
skipping items....

Nov 16 '05 #6

P: n/a
thank you

Jon Skeet [C# MVP] wrote:
MuZZy <le*******@yahoo.com> wrote:
Franck Diastein wrote:
Hi, I'm trying to remove items from a collection this way:

foreach(Object myO in ObjectCol){
if(myO != "xxx"){
myO.Remove();
}
}

But I'm having an error telling me that Collection was modified... I
understand this, since I remove one object from collection...

What is the best way to remove objects from my collection ?

TIA

What exactly class you are using? There's no ObjectCol class in
.NET,at least i didn't find one. If you are using like ArrayList,
then to remove object from collection just do:

foreach(Object o in al){
if(o != "xxx"){
al.Remove(o);
}
}

where al is your ArrayList object.

No, that's exactly the problem - that will throw an exception.

One way is to create a list of all objects you want to delete, then
remove then:

ArrayList removals = new ArrayList();
foreach (Object o in originalList)
{
if (o != "xxx")
{
removals.Add (o);
}
}

foreach (Object o in removals)
{
originalList.Remove(o);
}

Nov 16 '05 #7

P: n/a
Thank you

Bruce Wood wrote:
Don't forget that if you choose to use for instead of foreach, you must
loop _backward_ through the collection:

for (int i = myCollection.Length - 1; i >= 0; i--)
{
MyItemType item = (MyItemType)myCollection.Items[i];
if (item.Property == "xxx")
{
myCollection.Remove(item);
}
}

If you loop forward through the collection, then removing an item will
change the indices of all succeeding items, and you will end up
skipping items....

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.