1

I am getting this error in this line

Error Says:

system.invalidoperationexception collection was modified enumeration operation may not execute at System.Collections.Generic.Dictionary '2.ValueCollection.Enumerator.MoveNext<>

Line which I have error in

foreach (ISkill skill in client.Spells.Values)

When I tried to replace foreach with for I got three errors

UPDATE: My full code

 public static void SaveSpells(GameState client)
    {
        if (client.Fake) return;
        if (client.TransferedPlayer) return;
        if (((client.Entity != null) && (client.Spells != null)) && (client.Spells.Count != 0))
        {


            foreach (ISkill skill in client.Spells.Values)
            {
                DeadPool.Database.MySqlCommand command;
                if (skill.Available)
                {
                  //  if (skill.PreviousLevel != skill.Level)
                    {
                        command = new DeadPool.Database.MySqlCommand(MySqlCommandType.UPDATE);
                        command.Update("skills").Set("LevelHu2", skill.LevelHu2).Set("LevelHu", (long)skill.Souls).Set("Level", (long)skill.Level).Set("PreviousLevel", (long)skill.Level).Set("PreviousLevel", (long)skill.PreviousLevel).Set("Experience", (long)skill.Experience).Where("EntityID", (long)client.Entity.UID).And("ID", (long)skill.ID).Execute();
                    }
                }
                else
                {
                    skill.Available = true;
                    command = new DeadPool.Database.MySqlCommand(MySqlCommandType.INSERT);
                    command.Insert("skills").Set("LevelHu2", skill.LevelHu2).Set("LevelHu2", skill.LevelHu2).Insert("LevelHu", (long)skill.Souls).Insert("Level", (long)skill.Level).Insert("PreviousLevel", (long)skill.Level).Insert("Experience", (long)skill.Experience).Insert("EntityID", (long)client.Entity.UID).Insert("ID", (long)skill.ID).Execute();
                }
            }
        }
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kareem Nabil
  • 33
  • 1
  • 2
  • 9

1 Answers1

1

I suspect inside the loop you are modifying the collection, which is causing an exception.

foreach doesn't allow mutations on what you iterate, use for instead.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • When i use 'For' instead of 'foreach' i'm getting error 'Only assignment, call, increment, decrement, and new object expressions can be used as a statement' and also 2 others error which is '; expected' @Hari Prasad – Kareem Nabil Aug 23 '16 at 04:18
  • Can you update with the updated code(after changing it to for loop)? and highlight where you are getting error? – Hari Prasad Aug 23 '16 at 05:28