1

I want the code to show how many wagons there are and which animals are in each wagon. This is my error:

System.InvalidOperationException: "The collection has been changed. The inventory processing may not be performed. "

This is the code:

public IEnumerable<Animal> GetAnimals()
{
    return Animals.AsEnumerable();
}
public void Checker(List<Animal> listAnimals)
{
    foreach (Animal animal in listAnimals)
    {
        foreach (Wagon wagon in Wagons)
        {
            foreach (Animal wagonAnimal in wagon.GetAnimals())
            {
                if (wagon.StartCapacity <= wagon.MaxCapacity &&
                    animal.Formaat + wagon.StartCapacity <= wagon.MaxCapacity &&
                    wagonAnimal.Eater == Eater.carnivoor &&
                    animal.Eater == Eater.herbivoor &&
                    animal.Formaat >= wagonAnimal.Formaat)
                {
                    wagon.AddAnimal(animal);
                    Wagons.Add(wagon);    
                }
                else
                {
                     Wagon waggi = new Wagon();
                     waggi.AddAnimal(animal);
                     Wagons.Add(waggi);
                }
            }
        }

        Wagon wag = new Wagon();
        wag.AddAnimal(animal);
        Wagons.Add(wag);
    }
}

Can anyone give me some hints on this issue?

MR.T
  • 13
  • 5

3 Answers3

1

If you are looking to modify the collection while looping I would use a List object instead of an IEnumerable.

Some sample code would be like this:

List<Wagons> Wagons = new List<Wagons>
Wagons.AddAnimal(animal1);

foreach(Animal animal in Wagons.GetAnimals(){
   animal.Eater = Eater.herbivore;
}

Also looking at your code:

if (wagon.StartCapacity <= wagon.MaxCapacity &&
    animal.Formaat + wagon.StartCapacity <= 
    wagon.MaxCapacity && wagonAnimal.Eater == Eater.carnivoor &&
    animal.Eater == Eater.herbivoor && animal.Formaat >= wagonAnimal.Formaat)
{
    wagon.AddAnimal(animal);
    Wagons.Add(wagon);
} else {
    wagon.AddAnimal(animal);
    Wagons.Add(wagon);
}

This if/else statement does the exact same code, so you really don't need an if/else, you can just add the animal and add the wagon.

Lastly, shouldn't the parameter to your method accept a List or IEnumerable collection of wagons and not animals so you can loop through the wagons, and look through the animals in the wagons?

FranzHuber23
  • 3,311
  • 5
  • 24
  • 63
Jay Mason
  • 446
  • 3
  • 17
  • i changed the if/else statement but i still have the problem what i really dont understand very well. i made a list wagon private so and i made a list animal but the list animal is only to store them for the procedure and then after that put them in wagon – MR.T Mar 27 '19 at 14:18
  • Like NMSaiyed said below: You cannot add objects to a list while iterating over it with the `foreach` and `in` operator. I will try to answer your question. – FranzHuber23 Mar 27 '19 at 14:30
0

Actually you can't modify the list while looping. You need to create another object and add the wagon and animals respectively. Try this and comment if you didn't understand yet

0

You cannot modify a list while iterating over it using foreach and in.

Example:

foreach (Wagon wagon in Wagons)
{
    Wagon waggi = new Wagon();
    Wagons.Add(waggi);
}

will not work.

If you use e.g.

// This is needed to not get an endless loop (Because the length of the list
// increases after each time the Add() method is called to Wagons.)
int wagonCount = Wagons.Count;

for (int i = 0; i < wagonCount ; i++)
{
    Wagon waggi = new Wagon();
    waggi.AddAnimal(animal);
    Wagons.Add(waggi);
}

this will work.

My working example for your code (as far as I could get what you want to do is here: https://dotnetfiddle.net/6HXYmI and here: https://gist.github.com/SeppPenner/a082062d3ce2d5b8196bbf4618319044.

I would also recommend to check your code style according to the definitions from Microsoft: https://learn.microsoft.com/en-US/dotnet/csharp/programming-guide/inside-a-program/coding-conventions.

FranzHuber23
  • 3,311
  • 5
  • 24
  • 63
  • i have a problem because the list wagon starts with zero so the wagoncount is zero so the loop will do it one time and then its done how do i solve it then? – MR.T Mar 27 '19 at 16:23
  • `I want the code to show how many wagons there are and which animals are in each wagon` --> I don't really understand what you're trying to do. The code you provided does not count the wagons neither does it count the animals. You're trying to do somehting to add animals to the wagon(s). Can you provide some more details, please? – FranzHuber23 Mar 28 '19 at 07:47