Jump to content
  • Advertisement
Sign in to follow this  
Deltasquadron2

Removing Object from a list

This topic is 4385 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am trying to figure out a way to remove objects from a list without the need to create a 'remove' list. //Here is a working version: foreach(MovingObject o in movingObjects) { if (!o.FrameMove()) destroyedMovingObjects.Add(o); } foreach (MovingObject o in destroyedMovingObjects) { movingObjects.Remove(o); } destroyedMovingObjects.Clear(); ----------------------------------------------------------------------------- //Here is a version of my attempt: foreach(MovingObject o in movingObjects) { if (!o.FrameMove()) movingObjects.Remove(o); } /* foreach (MovingObject o in destroyedMovingObjects) { movingObjects.Remove(o); } destroyedMovingObjects.Clear(); */ ----------------------------------------------------------------------------- This is the error: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll Additional information: Collection was modified; enumeration operation may not execute. Thank you!!

Share this post


Link to post
Share on other sites
Advertisement
You are using foreach and then directly removing an item from your list. As the error states, calling Remove on the list will 'modify the list' and cause the enumerator to skip over items in the list - unexpected behavior - or point to some invalid location. Change the logic of your loop, or use the temporary list like in the "working" version.

Share this post


Link to post
Share on other sites
What is the difference between removing an object directly from the orginal list and removing it directly from a temporary list?

[Edited by - Deltasquadron2 on August 10, 2006 10:20:28 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Deltasquadron2
What is the difference between removing an object directly from the orginal list and removing it directly from a temporary list?


The problem is not that you're removing directly from the original list, but that you are removing from the list while enumerating through it. When you use foreach, you basically get an iterator into the list (represented by "o" in your example). This iterator points to the current object in the list. So now what happens when you remove the object being pointed at from the list? I personally don't know the details, but in any case, removing the current object invalidates the iterator. You can't continue to use it after that.

Hope that makes sense.

Share this post


Link to post
Share on other sites
This is C#, right?

Don't create a to-remove list. Instead, create a to-keep list, and then assign the list directly back over the original (rebind the variable to the new list):


foreach(MovingObject o in movingObjects)
{
if (o.FrameMove())
keptMovingObjects.Add(o);
}
movingObjects = keptMovingObjects;
// later, the old movingObjects nodes get garbage collected.

Share this post


Link to post
Share on other sites
Correct, it's C#.

I tried your suggestion and created a keepMovingObject list.

But for some reason the movingObjects = keepMovingObjects; is causing the same error:

Collection was modified; enumeration operation may not execute.

I made sure it was placed outside of the foreach loop. I also tried to use the copyTo method but that did not work either.

Share this post


Link to post
Share on other sites
Maybe this?


for (int i = 0;; i++)
{
if (i >= movingObjects.Count)
break;
if (!movingObjects.FrameMove())
{
movingObjects.Remove(movingObjects);
i--;
}

}


Haven't tested it yet though...

Share this post


Link to post
Share on other sites
Quote:
Original post by Deltasquadron2
Correct, it's C#.

I tried your suggestion and created a keepMovingObject list.

But for some reason the movingObjects = keepMovingObjects; is causing the same error:

Collection was modified; enumeration operation may not execute.

I made sure it was placed outside of the foreach loop. I also tried to use the copyTo method but that did not work either.


Zahlman's solution will work. I just coded up a quick example and it works fine:


List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
myList.Add(3);

List<int> keep = new List<int>();

foreach(int o in myList)
{
if (o == 2)
keep.Add(o);
}

myList = keep; // myList now correctly contains one item: myList[0] == 2

Share this post


Link to post
Share on other sites
Quote:
Original post by Deltasquadron2
Correct, it's C#.

I tried your suggestion and created a keepMovingObject list.

But for some reason the movingObjects = keepMovingObjects; is causing the same error:

Collection was modified; enumeration operation may not execute.

I made sure it was placed outside of the foreach loop. I also tried to use the copyTo method but that did not work either.


You're not by any chance doing any multithreading?

Beyond that, I'm not psychic - I can't tell you what the problem is, but the approach should work.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you are using C# 2.0 you can use the new RemoveAll method.

movingObjects.RemoveAll(new delegate(MovingObject movingObject)
{
return !(movingObject.FrameMove());
});

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!