473,473 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ListView iteration order

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,
--
Al Reid
Jul 10 '06 #1
2 2393

Al Reid wrote:
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.

--
Larry Lard
Replies to group please

Jul 10 '06 #2
Larry,

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

--
Al Reid

"Larry Lard" <la*******@hotmail.comwrote in message news:11**********************@b28g2000cwb.googlegr oups.com...
>
Al Reid wrote:
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.

--
Larry Lard
Replies to group please

Jul 10 '06 #3

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

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
0
by: Hunters | last post by:
Once you reorder the listview items, largeicon or smallicon view will not follow the order of list or detail view which is the correct order. For example with a listview as...
2
by: VR | last post by:
Is there any to right-justify the 0th (main/item's) column in ListView? I have a ListView control, which I send a LVM_SETCOLUMNORDERARRAY message to change the order of columns. Neither before...
2
by: Aaron Queenan | last post by:
Is there any way to know whether the OnItemCheck is being called in response to a user action (mouse or keyboard) as opposed to the form loading? I have a class which derives from...
13
by: Maheshkumar.R | last post by:
hi groups, I have placed an listview control, i want to iterate thru the control and find the clicked event items. listView2.Items.Add(fname.ToString(), i); how i can perform the iteration...
7
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I'v been struggeling with removing selected items from a listview. Anyone that can give me a piece of code that does this? I am a newbee to this C# and cant figure it out.... Regards...
5
by: billuy | last post by:
Is there a way to sort by a specified column in a listview? I'd like my users to be able to click on the column heading and have the listview sort by that column. Thanks, Billyb
0
by: thomasp | last post by:
I have a 2 listviews on a windows form in my VB2005 applicaion. The first listview is populated by the columns from a database table when the form opens. The user can move items back and forth...
11
by: Alan T | last post by:
Does the ListView supports sort? I want to have a up/down arrow/triangle that show it is sorted asc or desc on the column headers when I click the column header. May be I need a third-party...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.