ListView iteration order

A

Al Reid

I'm using VB 2005.

I have a production application where I load a ListView with information from several sources based on user interaction. At some
point I need to iterate through the Items collection and print the documents that have been requested. The contents of the ListView
are never sorted or manipulated in any way. Occasionally the documents print out of order.

Is it possible that:

For Each li as ListViewItem in DocsListView.Items
'...some code
Next

Will retrieve the ListView Items in an order that is different than:

For i as Integer = 0 to DocsListView.Items.Count -1
Dim li as ListViewItem = DocsListView.Items(i)
'...some code
Next

The problem seems to be infrequent, random and non-reproducible. Any thoughts before I modify the application?

TIA,
 
L

Larry Lard

Al said:
I'm using VB 2005.

I have a production application where I load a ListView with information from several sources based on user interaction. At some
point I need to iterate through the Items collection and print the documents that have been requested. The contents of the ListView
are never sorted or manipulated in any way. Occasionally the documents print out of order.

Is it possible that:

For Each li as ListViewItem in DocsListView.Items
'...some code
Next

Will retrieve the ListView Items in an order that is different than:

For i as Integer = 0 to DocsListView.Items.Count -1
Dim li as ListViewItem = DocsListView.Items(i)
'...some code
Next

Entirely within the realms of possibility. When you For Each over an
IEnumerable, the order you get the elements back is determined by the
implementation of the MoveNext method of the IEnumerator that the
IEnumerable's GetEnumerator returns. This implementation is entitled to
do basically whatever it wants - a perverse implementor would be
entirely within their rights to have the IEnumerator return the
elements in a different order each time an iteration was done, so long
as they fulfilled the contract, and only returned each element once
(and returned all the elements).
The problem seems to be infrequent, random and non-reproducible. Any thoughts before I modify the application?
From the docs for For Each:
Traversal Order. When you execute a For Each...Next loop, traversal of
the collection is under the control of the enumerator object returned
by the GetEnumerator method. The order of traversal is not determined
by Visual Basic, but rather by the MoveNext method of the enumerator
object. This means that you might not be able to predict which element
of the collection is the first to be returned in element, or which is
the next to be returned after a given element.

If your code depends on traversing a collection in a particular order,
a For Each...Next loop is not the best choice unless you know the
characteristics of the enumerator object the collection exposes. You
might achieve more reliable results using a different loop structure,
such as For...Next or Do...Loop.
If the order matters, use an indexer.
 
A

Al Reid

Larry,

Thanks for the info. I thought this may be the case. I had never run into this before switching to .Net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top