集合被修改枚举操作可能无法执行VB .net [英] collection was modified enumeration operation may not execute VB .net

查看:72
本文介绍了集合被修改枚举操作可能无法执行VB .net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sub Procedure接受过滤的用户集合作为参数,我想从该集合中获取userID的值,



我可以看到它成功添加第一个userID,但在Next中引发异常。



I have a Sub Procedure which accepts a filtered users collection as parameter and i would like to get the value of userID from that collection,

I can see it successfully add's the first userID but in the Next it raises an exception.

Private Sub GetUserID(ByRef filteredusers As Business.HR.UserCollection)
If filteredusers IsNot Nothing Then
 For Each uIDs As Business.HR.User In filteredusers
    filteredusers.Add(uIDs.UserId)
  Next
  'exception here as System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
End If
End Sub





请继续帮助。



Kindly help to proceed.

推荐答案

问题是你是在迭代过滤的用户,再次在循环内你将项目添加到同一个集合 filteredusers.Add(uIDs.UserId),迭代时无法更改相同的集合。

problem is you are iterating over filteredusers and again inside the loop you are adding items to same collection filteredusers.Add(uIDs.UserId), you can't change the same collection while iterating.
Private Sub GetUserID(ByRef filteredusers As Business.HR.UserCollection)
    ' if uIDs.UserId is integer create list of integers
    Dim ids As New List(Of Integer)()
    If filteredusers IsNot Nothing Then
        For Each uIDs As Business.HR.User In filteredusers
            ids.Add(uIDs.UserId)

        Next
    End If
End Sub


这篇关于集合被修改枚举操作可能无法执行VB .net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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