2016-08-23 57 views
-1
執行

我得到這個錯誤在這行system.invalidoperationexception集合被修改枚舉操作可能無法System.Collections.Generic.Dictionary

錯誤說:

system.invalidoperationexception集合被修改枚舉操作可能無法在System.Collections.Generic.Dictionary「2.ValueCollection.Enumerator.MoveNext執行<>

線,我有錯誤在

foreach (ISkill skill in client.Spells.Values) 

當我試圖用for更換foreach我有三個錯誤

UPDATE:我的全碼

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(); 
       } 
      } 
     } 
    } 
+1

迭代時,不要在'client.Spells'中添加或刪除。這也包括來自不同的線程。並且,記得先搜索.. – user2864740

+0

你是什麼意思,我不明白你兄弟 –

回答

0

我在循環裏懷疑你正在修改的集合,這是引起例外。

foreach不允許突變你的迭代,用for代替。

+0

當我使用'For'而不是'foreach'我得到錯誤'只有賦值,調用,增量,減量和新對象表達式可以作爲一個語句使用,也可以用作其他兩個錯誤';預計'@Hari Prasad –

+0

除非我看到您的代碼,否則難以發表評論,根據我的經驗,您的新錯誤可能是因爲將方法和屬性互換,這是沒有意義的。 –

+0

將我的代碼添加到主題@Hari Prasad –

相關問題