从收集的收集迭代过程中的异常和删除项目 [英] Exception during iteration on collection and remove items from that collection

查看:164
本文介绍了从收集的收集迭代过程中的异常和删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从ArrayList中的foreach循环除去项目,并得到follwing例外



集合已修改;枚举操作可能无法执行。



我怎么能删除的foreach项目,



编辑: *的有可能是一个项目删除或两个或全部的*



以下是我的代码:

  / * 
*需要从attachementsFielPath,它并不在名字数组中存在删除所有项目。
* /


{
串attachmentFileNames = txtAttachment.Text.Trim(); //有文件名的文本框。
的String [] =名称attachmentFileNames.Split(新的char [] {','});

INT指数= 0;

// attachmentsFilePath是ArrayList中持有任何时候选择fiels用户的完整路径。
的foreach(在attachmentsFilePath VAR fullFilePath)
{
布尔isNeedToRemove = TRUE;从完整路径

//提取文件名。
字符串文件名= fullFilePath.ToString()子串(fullFilePath.ToString()LastIndexOf('\\')+ 1)。

的for(int i = 0; I< names.Length;我++)
{
//如果阵列中找到的文件名则不需要检查剩余的项目。
如果(fileName.Equals(名称[I] .Trim()))
{
isNeedToRemove = FALSE;
中断;
}
}

//如果名称不数组找到的文件,将其删除。
如果(isNeedToRemove)
{
attachmentsFilePath.RemoveAt(指数);
isNeedToRemove = TRUE;
}

指数++;
}
}
赶上(异常前)
{
罚球前;
}



编辑:在代码可以也咨询。我是否需要将其拆分成小的方法和异常处理等。



无效参数异常在从ArrayList中创建泛型列表



 的foreach(在新的List< VAR fullFilePath;串>(attachmentsFilePath))

{





当我使用列表< ArrayList的> 的例外是
参数1 System.Collections.ArrayList:不能将''到'廉政'



attachmentsFilePath声明如下

  ArrayList的attachmentsFilePath =新的ArrayList(); 



但是,当我宣布它是这样,问题就迎刃而解了。



 列表< ArrayList的> attachmentsFilePath =新的List< ArrayList的>(); 


解决方案

您可以遍历集合的副本:

 的foreach 
{
//做的东西$ b $(新的ArrayList(attachmentsFilePath)VAR fullFilePath) b}


I remove item from ArrayList in foreach loop and get follwing exception.

Collection was modified; enumeration operation may not execute.

How can I remove items in foreach,

EDIT: *There might be one item to remove or two or all.*

Following is my code:

/*
 * Need to remove all items from 'attachementsFielPath' which does not exist in names array.
 */

try
{
    string attachmentFileNames = txtAttachment.Text.Trim(); // Textbox having file names.
    string[] names = attachmentFileNames.Split(new char[] { ';' });

    int index = 0;

    // attachmentsFilePath is ArrayList holding full path of fiels user selected at any time.
    foreach (var fullFilePath in attachmentsFilePath)
    {
        bool isNeedToRemove = true;

        // Extract filename from full path.
        string fileName = fullFilePath.ToString().Substring(fullFilePath.ToString().LastIndexOf('\\') + 1);

        for (int i = 0; i < names.Length; i++)
        {
        // If filename found in array then no need to check remaining items.
        if (fileName.Equals(names[i].Trim()))
        {
            isNeedToRemove = false;
            break;
        }
        }

        // If file not found in names array, remove it.
        if (isNeedToRemove)
        {
        attachmentsFilePath.RemoveAt(index);
        isNeedToRemove = true;
        }

        index++;
    }
}
catch (Exception ex)
{
    throw ex;
}

EDIT: Can you also advice on code. Do I need to break it into small methods and exception handling etc.

Invalid argument exception On creating generic list from ArrayList

foreach (var fullFilePath in new List<string>(attachmentsFilePath))

{

When I use List<ArrayList> the exception is Argument '1': cannot convert from 'System.Collections.ArrayList' to 'int'

attachmentsFilePath is declared like this

ArrayList attachmentsFilePath = new ArrayList();

But when I declared it like this, problem solved

List<ArrayList> attachmentsFilePath = new List<ArrayList>();

解决方案

You can iterate over a copy of the collection:

foreach(var fullFilePath in new ArrayList(attachmentsFilePath))
{
    // do stuff
}

这篇关于从收集的收集迭代过程中的异常和删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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