-8

The problem I am having is that I have created a list with an object inside but when the player collides with the Object in question I can't remove it from the list.

//This is where the issue is
foreach (Objective seagull in seagulls)
{
    if (Objective.visable == false)
    {
        seagulls.Remove(seagull);
    }
}

EDIT: My problem now after changing to a for loop is that the remove does not work, I have no Idea what the remove function is asking me for ?

001
  • 13,291
  • 5
  • 35
  • 66
Adam
  • 1
  • 1
  • 5
    Please show your code and exception message as a text in question itself. People don't need to follow 3rd party sites to see your code. – Soner Gönül Nov 23 '15 at 14:26
  • I can't there will be too much code? – Adam Nov 23 '15 at 14:27
  • And try to simplify the code as much as possible to fastly get where you are stuck. With the current source-code it may take much time to understand what you´re doing and where the actual problem is. Furthermore when you click on details within the debugger when showing the sexception, what do you get there? Is there a stacktrace? – MakePeaceGreatAgain Nov 23 '15 at 14:27
  • 4
    you cannot modify a collection while looping through it with a `foreach` – Jonesopolis Nov 23 '15 at 14:27
  • 2
    Possible duplicate of [How to remove elements from a generic list while iterating over it?](http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it) – rory.ap Nov 23 '15 at 14:28
  • 1
    Don't use `foreach`, use `for` with an inverse indexer and removed the last. – Stefan Nov 23 '15 at 14:28
  • See : http://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute – PaulF Nov 23 '15 at 14:29
  • Could you explain inverse indexer please? – Adam Nov 23 '15 at 14:31
  • We don't need your entire application, just the relevant code. Pastebin is blocked at my work (and the job I had before this) so I can't look at your code. No reason to post the exception as a screenshot either. Loop through it backwards; `for (int i = myList.Count - 1; i >= 0; i--) myList[i].Remove` or whatever it would be – sab669 Nov 23 '15 at 14:31
  • Sorry, First time using Stack. Didn't know what was needed. And Thanks for the help everyone – Adam Nov 23 '15 at 14:33
  • @Adam take a look at the help section, in particular this: http://stackoverflow.com/help/how-to-ask – rory.ap Nov 23 '15 at 14:36
  • In the new `for` loop, you should [`RemoveAt`](https://msdn.microsoft.com/en-us/library/5cw9x18z(v=vs.110).aspx) – 001 Nov 23 '15 at 14:43
  • I have used RemoveAt(i), But the object is still there. – Adam Nov 23 '15 at 14:47

2 Answers2

2

When using a for, you should use RemoveAt since it will reference your index. Remove will expect a value to match, this is may be partially why your new for implementation doesn't work.


The reason you're receiving that error, is because you're enumerating through a collection that was modified. If you utilize a for loop, instead of foreach you'll be able to iterate through while removing particular indexes. Otherwise, you'll need to create a separate List, which is a copy of your List. Remove it, then equal it to the other.

int total = example.Count;
List<Example> example;
for(int i = 0; i < total; i++)
     if(...)
     {
           example.RemoveAt(i);
     }

The conditional will be your check, before removing. I'm not sure what that would be, but it would be similar to that. I simplified code a bit for brevity sake.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • Your example works, but just to clarify. If the object is removed from the list does that mean it stops excising therefor all the logic for that object stops working. Because allthough I am not getting any errors anymore the score continues to rise as if the object was there. – Adam Nov 23 '15 at 14:41
  • Here is the new code, if (Player.playerBound.Intersects(Objective.objectiveBound)) { Objective.visable = false; Player.score = Player.score + 10; for (int i = seagulls.Count - 1; i >= 0; i--) { if (Objective.visable == false) { seagulls.RemoveAt(i); } } } – Adam Nov 23 '15 at 14:55
0

Yo can put some flag like 'active' in your 'seagull' and then remove all

seagulls.removeall(c => c.active == false) 
Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26