In article <ub**************@TK2MSFTNGP10.phx.gbl>, Armin Zingler wrote:
"Ty Moffett" <tm******@solarc.com> schrieb Is there anything obvious wrong with this little loop? The second
statement throws an exception "Enumeration has not started. Call
MoveNext." I thought that is what I did in line one.
My intent here is to take what is left in the queue and write it back
to the text file from which it came so it can survive an upcoming
reboot.
Do While myQ.GetEnumerator.MoveNext
sw = myQ.GetEnumerator.Current 'sw is a StreamWriter
Loop
I guess, each call to GetEnumerator creates a _new_ enumerator. Solution:
dim en as ienuemrator
en = myQ.GetEnumerator
Do While en.MoveNext
sw = em.Current 'sw is a StreamWriter
Loop
I have to ask... Why don't you just use a For Each?
Dim q As Queue(New String() {"1", "2", "3", "4", "5"}
For Each s As String In q
Console.WriteLine(s)
Next
Unless your trying to actually modify the collection (like calling
Enqueue or Dequeue), that should work fine. And if you are trying to
modify the collection, then you shouldn't use the enumerator anyway
because it's going to throw an exception. In that case, you should just
do something like:
Do While q.Count > 0
s = DirectCast(q.Dequeue(), String)
Console.WriteLine(s)
Loop
--
Tom Shelton
MVP [Visual Basic]