这个枚举器绑定的列表已被修改 [英] List that this enumerator is bound to has been modified

查看:112
本文介绍了这个枚举器绑定的列表已被修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些代码,在下一个语句中给我一个错误:
$ b


列举这个枚举器是必然会被修改。一个枚举器只能用在列表没有改变的情况下。


我真的不知道如何进一步解释这个问题,如果你需要我,我可以尝试。

 对于每个itemChecked在storedAuthorsListbox.CheckedItems 
Dim selectedAuthor As String = storedAuthorsListbox .SelectedItem.ToString()
Dim authorFile As String =Authors\& itemChecked.ToString()
Dim document As XmlReader = New XmlTextReader(authorFile)

metaInfo& =[Author]& vbNewLine
$ b while(document.Read())
Dim type = document.NodeType
If(type = XmlNodeType.Element)Then
If(document.Name = Name)然后
metaInfo& =Name =& document.ReadInnerXml.ToString()& vbNewLine
ElseIf(document.Name =Website)然后
metaInfo& =Website =& document.ReadInnerXml.ToString()& vbNewLine
ElseIf(document.Name =Notes)然后
metaInfo& =Notes =& document.ReadInnerXml.ToString()& vbNewLine
End If
End If
End While
document.Close()
Next


解决方案

有些代码在迭代时正在改变storedAuthorsListbox。该代码在代码片段中不可见。确保发布的代码不在工作线程中运行,这是不合法的。它肯定是呱呱叫,就像你在工作人员中运行的那种代码一样。



通用的解决方案是复制这些项目,控制:
$ b $ pre $ Dim copy = storedAuthorsListBox.SelectedItems.OfType(Of String)()。ToList()
For Each itemchecked在复制
等等。
下一个

一个工作线程然后将副本传递给工作人员。


I have this bit of code here, and at the next statement it's giving me an error saying:

List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

I really don't know how to further explain this issue, but if you need me to I can try.

For Each itemChecked In storedAuthorsListbox.CheckedItems
  Dim selectedAuthor As String = storedAuthorsListbox.SelectedItem.ToString()
  Dim authorFile As String = "Authors\" & itemChecked.ToString()
  Dim document As XmlReader = New XmlTextReader(authorFile)

  metaInfo &= "[Author]" & vbNewLine

  While (document.Read())
    Dim type = document.NodeType
    If (type = XmlNodeType.Element) Then
      If (document.Name = "Name") Then
        metaInfo &= "Name=" & document.ReadInnerXml.ToString() & vbNewLine
      ElseIf (document.Name = "Website") Then
        metaInfo &= "Website=" & document.ReadInnerXml.ToString() & vbNewLine
      ElseIf (document.Name = "Notes") Then
        metaInfo &= "Notes=" & document.ReadInnerXml.ToString() & vbNewLine
      End If
    End If
  End While
  document.Close()
Next

解决方案

Some code somewhere is changing storedAuthorsListbox while you are iterating it. That code is not visible in the snippet. Do make sure that the posted code is not running in a worker thread, that is not legal. It certainly quacks like the kind of code you'd run in a worker.

The generic solution is to make a copy of the items and work from that copy instead of the control:

    Dim copy = storedAuthorsListBox.SelectedItems.OfType(Of String)().ToList()
    For Each itemchecked In copy
        '' etc..
    Next

If this runs in a worker thread then pass the copy to the worker.

这篇关于这个枚举器绑定的列表已被修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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