vb.net中的枚举

时间:2010-01-16 09:26:04

标签: vb.net

执行以下行时我遇到了错误。错误:

Collection was modified; enumeration operation may not execute. 

帮我解决这个问题。

Dim i As IEnumerator
    Dim item As DataGridItem
    Dim bChk As Boolean = False
    i = dgOfferStatus.Items.GetEnumerator
    For Each item In dgOfferStatus.Items
        i.MoveNext()
        item = i.Current
        item = CType(i.Current, DataGridItem)
        Dim chkItemChecked As New CheckBox
        chkItemChecked = CType(item.FindControl("chkItemChecked"), CheckBox)
        If chkItemChecked.Checked = True Then
            Try
                bChk = True
                lo_ClsInterviewProcess.JobAppID = item.Cells(1).Text
                lo_ClsInterviewProcess.candId = item.Cells(9).Text
                Dim str, strSchedule1, strSchedule As String
                Dim dspath As DataSet
                Dim candidateId As Integer
                ''Moving the resume to Completed folder

                ObjInterviewAssessment = New ClsInterviewAssessment
                dspath = ObjInterviewAssessment.GetOffComPath(CInt(lo_ClsInterviewProcess.JobAppID), "GetHoldPath")
                If dspath.Tables(0).Rows.Count > 0 Then
                    If Not IsDBNull(dspath.Tables(0).Rows(0).Item(0)) Then
                        str = dspath.Tables(0).Rows(0).Item(0)
                        strSchedule1 = str.Replace("Hold", "Completed")
                    End If
                End If

                Dim str1 As String
                str1 = Server.MapPath(str).Trim
                strSchedule = Server.MapPath(strSchedule1).Trim

                Dim file1 As File
                If file1.Exists(str1) Then
                    If file1.Exists(strSchedule) Then
                        file1.Delete(strSchedule)
                    End If

                    file1.Move(str1, strSchedule)
                End If
                ''
                intResult = lo_ClsInterviewProcess.UpdateApproveStatus(Session("EmployeeId"), strSchedule1)
                BindHoldGrid()
                If intResult > 0 Then


                    Alert.UserMsgBox("btnsearch", "Status Updated")
                Else
                    Alert.UserMsgBox("btnsearch", "Status not Updated")
                End If
            Catch ex As Exception
                ExceptionManager.Publish(ex)
                Throw (ex)
            End Try
        End If
    Next
    If bChk = False Then
        Alert.UserMsgBox("btnsearch", "Please Select any Candidate")
    End If
    'Catch ex As Exception
    '    ExceptionManager.Publish(ex)
    'End Try
End Sub

1 个答案:

答案 0 :(得分:4)

看看你的代码的这一部分。我认为这是导致你例外的原因。

Dim i As IEnumerator
...
Dim item As DataGridItem
...
i = dgOfferStatus.Items.GetEnumerator
For Each item In dgOfferStatus.Items
    i.MoveNext()
    item = i.Current  ' <-- here be dragons!? '
    ...
Next

你在做什么似乎有点奇怪。您遍历同一个集合(dgOfferStatus.Items)两次,一次使用For Each循环,一次手动使用i迭代器。然后,使用item = i.Current修改集合中的项目。我相信这是导致异常的分配。

(我也不明白为什么你会这样做。这个赋值似乎是完全多余的,因为i.Currentitem应该是相同的,因为两个迭代器都在集合中的相同位置。)

该异常基本上试图告诉您在迭代时可能无法修改集合。但你似乎正是这样做的。