How to use IEnumerator.Reset with generic collection in VB.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

From the documentation in MSDN, it looks as though the following should
iterate through the collection twice. However, MoveNext in the second
iteration returns false:
Dim oList As New List(Of String)
oList.Add("Hello")
oList.Add("world")
Dim oEnum As List(Of String).Enumerator = oList.GetEnumerator()
While oEnum.MoveNext()
Dim sValue As String = oEnum.Current
End While
CType(oEnum, IEnumerator).Reset()
While oEnum.MoveNext()
Dim sValue As String = oEnum.Current
End While
Anyone know what I am doing wrong?
 
wvtempl said:
From the documentation in MSDN, it looks as though the following should
iterate through the collection twice. However, MoveNext in the second
iteration returns false:
Dim oList As New List(Of String)
oList.Add("Hello")
oList.Add("world")
Dim oEnum As List(Of String).Enumerator = oList.GetEnumerator()
While oEnum.MoveNext()
Dim sValue As String = oEnum.Current
End While
CType(oEnum, IEnumerator).Reset()
Try oEnum = oList.GetEnumerator() instead to start over with a brand new
enumerator.
Or just use For Each and let the compiler take care of the enumerators.
 
Thanks, getting a new enumerator is the workaround I have been using. Does
anyone know if the Reset method simply doesn't work, or if the way I am
calling it is incorrect?
 
Back
Top