473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Queue.GetEnumerator

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
Nov 20 '05 #1
9 4154
"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
--
Armin

Nov 20 '05 #2
* "Ty Moffett" <tm******@solarc.com> scripsit:
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
///
Loop


--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
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!

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bn************@ID-208219.news.uni-berlin.de...
* "Ty Moffett" <tm******@solarc.com> scripsit:
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
///
Loop


--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #4
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]
Nov 20 '05 #5
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

"Ty Moffett" <tm******@solarc.com> wrote in message
news:uu****************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #6
"Tom Shelton" <to*@mtogden.com> schrieb
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?


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.
--
Armin

Nov 20 '05 #7
On 2003-10-23, Armin Zingler <az*******@freenet.de> wrote:
"Tom Shelton" <to*@mtogden.com> schrieb
> 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?


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
MVP [Visual Basic]
Nov 20 '05 #8
"Tom Shelton" <to*@mtogden.com> schrieb
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. ;-)

--
Armin

Nov 20 '05 #9
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. =)

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"Ty Moffett" <tm******@solarc.com> wrote in message
news:uu****************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4905
by: Kevin P. Fleming | last post by:
ArrayList implements an overload for IEnumerator's GetEnumerator method as follows: GetEnumerator(int offset, int count) However, I can't see a way to take advantage of this, even though I'd...
4
5072
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
2
1551
by: Sam Marrocco | last post by:
I've constructed a class that inherits the NameObjectCollectionBase class. All works well, but I'd like to shadow the GetEnumerator method so that it returns an actual value *instead of a...
4
8170
by: nhmark64 | last post by:
Hi, Does System.Collections.Generic.Queue not have a Synchronized method because it is already in effect synchronized, or is the Synchronized functionality missing from...
2
19850
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return",...
3
3728
by: =?Utf-8?B?cmNoZg==?= | last post by:
I know this is simple problem but I am so new that there is some fundamental disconnect in my understanding of: Enumerable, Enumerator, and Generics. As a result I am having a problem debugging...
2
1357
by: =?Utf-8?B?VGVk?= | last post by:
I have been asked to change an existing program which Implemented a Collection Class The current class had what is below (its just a summary of the code) Public Class FieldList Implements...
16
4244
by: colin | last post by:
Hi, is it possible to have a recursive GetEnumerator for traversing a tree structure ? public IEnumerator<DTypeGetEnumerator() { return GetEnumerator(root);
2
2168
by: Tony | last post by:
Hello! According to the documentation we have the following interface IList : ICollection, IEnumerable { I can understand all the methods in this IList } interface ICollection : IEnumerable...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.