|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ListView iteration orderI 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 Al Reid wrote:
Show quoteHide quote > I'm using VB 2005. Entirely within the realms of possibility. When you For Each over an> > 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 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? Traversal Order. When you execute a For Each...Next loop, traversal of>From the docs for For Each: >> 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 Larry,
Thanks for the info. I thought this may be the case. I had never run into this before switching to .Net. -- Show quoteHide quoteAl Reid "Larry Lard" <larryl***@hotmail.com> wrote in message news:1152537042.156706.112990@b28g2000cwb.googlegroups.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 >
Redrawing graphics in userControl - Button
How to use ADO command object to get a record set via stored procedure? start new thread help A question about sockets, listener VS 2005 Property question datasets web browser control auto scroll How to convert ShortPath to longPath? Internet Transfer Control in Dot Net??? |
|||||||||||||||||||||||