5

If I use an item in a foreach loop and I can't use the item it has to delete the item that is currently in the foreach loop.

This is the code that I have right now:

foreach (Line line in linelijst)
{
    try
    {
        if (line.ActorIndex() == 0)
        {
            line.setStartPoint(actorenlijst[0].getLinePoint()); //if actorenlijst[0] doesn't excist it has to delete the current line
        }
        if (line.ActorIndex() == 1)
        {
             line.setStartPoint(actorenlijst[1].getLinePoint()); //if actorenlijst[1] doesn't excist it has to delete the current line
        }
        if (line.ActorIndex() == 2)
        {
             line.setStartPoint(actorenlijst[2].getLinePoint()); //if actorenlijst[2] doesn't excist it has to delete the current line
        }
        Point start = line.getStartPoint();
        Point end = line.getEndPoint();
        Pen lijn = new Pen(Color.Black, 1);
        graphics.DrawLine(lijn, start, end);
    }
    catch
    {
         //delete current line from the list
    }
}

Thanks for your interest to help other people :)

NASSER
  • 5,900
  • 7
  • 38
  • 57
Lesley Peters
  • 409
  • 1
  • 5
  • 20
  • That catch looks well dodgy. What if the exception was caused by a coding error, e.g. `NullReferenceException`? You're going to silently remove that line from the array? – Matthew Watson Sep 10 '15 at 13:53
  • I just got the right answer : .ToList() Can't find a better answer anywhere else but now I see that it's deleted. – Lesley Peters Sep 10 '15 at 14:04

2 Answers2

10

Try just creating another temporary list for the items that need to be deleted then when your done looping you can just delete the ones in the temp list.

List<Type> temp = new List<Type>()
foreach(item in mainList)
{
   if (item.Delete)
   {
      temp.Add(item);
   }
}

foreach (var item in temp)
{
   mainList.Remove(item);
}
kakashi21
  • 116
  • 3
3

You canNOT change the listing through which you are going. It is locked because it is an Enumeration as long as it is in the foreach. So use a for-loop instead.

for (int i = linelijst.count; i > 0; i--)
{
    // linelijst[i - 1] can be removed, etc.
}

or use (as the commentators suggested):

for (int i = linelijst.count - 1; i >= 0; i--)
{
    // linelijst[i] can be removed, etc.
}
Marcel Grüger
  • 885
  • 1
  • 9
  • 25
  • 5
    Should be noted that when the item is removed you'd want to subtract one from the index so you don't skip anything, or if possible iterate from the end of the list instead. – juharr Sep 10 '15 at 13:57
  • 1
    for (int i = linelijst.count - 1; i >= 0; i--) or i might be out of range. – cheny Feb 11 '22 at 13:21
  • Because of that I use >0 not >= 0. So I don't need the -1. – Marcel Grüger Feb 14 '22 at 10:03
  • 1
    To use the code above you have to use linelijst[ i - 1] when referencing the item in the body of the for loop. Because of that it is probably preferable to have: `for (int i = linelijst.count - 1; i >= 0; i--)` as @cheny suggests – David Coster Jun 23 '22 at 05:58