收集被修改; [英] Getting collection was modified;

查看:74
本文介绍了收集被修改;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我收到的是一个收藏品被修改过;枚举操作可能无法执行。错误。 Everyting工作正常并且保存我只是得到了错误仍然是错误的。

  public   static   void  addItem(SocketUser用户,SocketGuild公会,Item项目, int 金额)
{
checkUser(用户,公会);
if (getItemList(guild).ContainsKey(item.Id)){
List< localItem> inventory = getPlayerInventory(user,guild);
List< string> playerList = getInventoryList(guild);
if (inventoryContains(item,inventory)){
localItem local = new localItem();
local = getLocalItem(inventory,item);
inventory.Remove(getLocalItem(inventory,item));
local.amount + =金额;
inventory.Add(local);
}
else
{
localItem local = new localItem();
local.item = item;
local.amount + =金额;
inventory.Add(local);
}
foreach string line in playerList)
{
if (line.Contains(user.Id.ToString()))
{
string newline = embedInventoryString(user.Id.ToString(),inventory);
playerList.Remove(line);
playerList.Add(换行符);
File.WriteAllLines( server data \\inventoryData + guild.Id。 ToString()+ .json,playerList);
Console.WriteLine( done 2);
}
}
}
}





我尝试了什么:



我试过移动File.WriteAllLines,因为我认为它只是在错误的时间被修改,我没看到代码有什么问题?

解决方案

迭代时你不能修改集合 playerList 通过它使用 foreach 。 相反,使用索引器遍历列表。



/ ravi


要添加Ravi所说的,这也是一个非常低效的这样做的方法。

看看这里:列表与LT; T> - 它是否真的像您想象的那样高效? [ ^ ]



我的任务是构造一个正确大小的新 playerList ,并将旧元素复制过来,或用新元素替换它。


Hey guys, I'm getting a Collection was modified; enumeration operation may not execute. error. Everyting is working fine and saving i just get spat that error still.

public static void addItem(SocketUser user, SocketGuild guild, Item item, int amount)
{
    checkUser(user, guild);
    if (getItemList(guild).ContainsKey(item.Id)) {
        List<localItem> inventory = getPlayerInventory(user, guild);
        List<string> playerList = getInventoryList(guild);
        if (inventoryContains(item, inventory)) {
            localItem local = new localItem();
            local = getLocalItem(inventory, item);
            inventory.Remove(getLocalItem(inventory, item));
            local.amount += amount;
            inventory.Add(local);
        }
        else
        {
            localItem local = new localItem();
            local.item = item;
            local.amount += amount;
            inventory.Add(local);
        }
        foreach (string line in playerList)
        {
            if (line.Contains(user.Id.ToString()))
            {
                string newline = embedInventoryString(user.Id.ToString(), inventory);
                playerList.Remove(line);
                playerList.Add(newline);
                File.WriteAllLines("server data\\inventoryData" + guild.Id.ToString() + ".json", playerList);
                Console.WriteLine("done 2");
            }
        }
    }
}



What I have tried:

I've tried moving the File.WriteAllLines around because i was thinking it was just being modified at the wrong time, i don't see anything wrong with the code?

解决方案

You can't modify the collection playerList while iterating through it using foreach.  Instead, walk the list using an indexer.

/ravi


To add to what Ravi said, that's also a very inefficient way to do it.
Have a look here: List<T> - Is it really as efficient as you probably think?[^]

The way I would do your task is to construct a new playerList of the right size and either copy the old element over, or replace it with the new one.


这篇关于收集被修改;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆