Queue.GetEnumerator

  • Thread starter Thread starter Ty Moffett
  • Start date Start date
T

Ty Moffett

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
 
Ty Moffett said:
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
 
* "Ty Moffett said:
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.

\\\
Dim e As IEnumerator = myQ.GetEnumerator()
///
Do While myQ.GetEnumerator.MoveNext

Instead of the line above:

\\\
Do While e.MoveNext()
///
sw = myQ.GetEnumerator.Current 'sw is a StreamWriter

Use this instead of the line above:

\\\
sw = e.Current
///
 
Thanks a lot fellas. This little app is really coming along.

I did fnd that sw.WriteLine(en.Current) worked like I wanted it too.

Thanks again!
 
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 said:
I have to ask... Why don't you just use a For Each?

Because Ty Moffett also didn't. Of course I could also point him to For
each, but I wanted to show why the error occurs.
 
Ty,
Why are using a While loop instead of a For Next?

As Armin stated, each call to GetEnumertor returns a new IEnumerator object,
as you have multiple threads and each thread can be enumerating over the
same collection, or even have a single thread & can be enumerating over the
same collection with multiple IEnumerator variables.

I would recommend you use a For Each:

' VS.NET 2003 syntax
For Each item As Object In myQ
sw.WriteLine(item)
Next

Hope this helps
Jay
 
Because Ty Moffett also didn't. Of course I could also point him to For
each, but I wanted to show why the error occurs.

Sorry, Armin... My post was acutally supposed to be attached to Ty's
but I some how muffed it... I realize you were just showing him how it
works. Gosh, that's the second time today that I've felt sheepish. I
think I better take a day off :)
 
Tom Shelton said:
Sorry, Armin... My post was acutally supposed to be attached to
Ty's but I some how muffed it... I realize you were just showing him
how it works.


No problem. Happens to everyone now and then. :-)
Gosh, that's the second time today that I've felt
sheepish. I think I better take a day off :)

Sometimes I think we all should take a day off. ;-)
 
Mainly just out of ignorance. =) I'm going to take your advice and try to
simplify my code.

Thanks a lot for all of you help guys. =)
 
Back
Top