0

I have a method that iterates through a list of objects and if it finds one that matches a certain criterion, it is supposed to delete it:

public static void MakeChanges(MappingFileModel modelWithChanges)
{
    foreach (var mapping in Mapping)
    {
        if (mapping.ScanIndex.Equals(modelWithChanges.ScanIndex))
        {
            if (modelWithChanges.Column == -1)
            {
                Mapping.Remove(mapping);
            }
        }
    }
}

Fairly simple, I would have thought: The method takes an instance of a MappingFileModel called modelWithChanges, then iterates over the list, finds the instance where the ScanIndex strings match. It then checks if the Column == -1, (I've deleted the else statements for other options for brevity). So, if those two if statements evaluate to true, I want to remove mapping from the list Mapping.

Problem is, it doesn't remove it. I doesn't throw an error, either. It just doesn't do it. It does hit the statement if I set a breakpoint. I have also tried:

Mapping.RemoveAt(Mapping.IndexOf(mapping));

That does through an Index out of range error, which would indicate that my code can't locate mapping in Mapping. That makes no sense, since I am iterating over Mapping?

Update:

Thanks to pointing out the other article... I edited my code to the following:

public static void MakeChanges(MappingFileModel modelWithChanges)
    {
        for (var i = 0; i<Mapping.Count; i++)
        {
            if (Mapping[i].ScanIndex.Equals(modelWithChanges.ScanIndex))
            {
                if (modelWithChanges.Column == -1)
                {
                    Mapping.RemoveAt(i);
                }
            }
        }
    }

It still doesn't remove that object...

Update 2:

Ok, so I have now gone one step further and created a variable outside the loop... and it still doesn't delete the object. It's there. There is no error.

public static void MakeChanges(MappingFileModel modelWithChanges)
    {
        var deleteIndex = -1;

        for (var i =0; i<Mapping.Count; i++)
        {
            if (Mapping[i].ScanIndex.Equals(modelWithChanges.ScanIndex))
            {
                if (modelWithChanges.Column == -1)
                {
                    deleteIndex = i;
                }
            }
        }

        if (deleteIndex != -1)
        {
            Mapping.RemoveAt(deleteIndex);
        }
    }
4ndy
  • 550
  • 1
  • 9
  • 23

1 Answers1

1

You cannot make changes to a collection while iterating over it using foreach. If you want to update it, then try using for loop.

For more details, refer to this.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
  • Thank you (facepalm). I forgot about that! – 4ndy Sep 29 '17 at 06:36
  • Thanks to overeager diiN_______, and HimBromBeere, who instead of looking at the problem, just stamped it as duplicate... I found the bug, and it had nothing to do with the code I presented. For those who are looking for a solution to this, if they come across this: The problem was with the list itself. It was a static property of a class and the getter kept refreshing the contents of the list whenever it was requested. It didn't matter how often I changed the list, the getter always refreshed it again. – 4ndy Sep 29 '17 at 22:59