ListView iteration order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Al Reid

    #1

    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.It ems
    '...some code
    Next

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

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

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

    TIA,
    --
    Al Reid


  • Larry Lard

    #2
    Re: ListView iteration order


    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.It ems
    '...some code
    Next
    >
    Will retrieve the ListView Items in an order that is different than:
    >
    For i as Integer = 0 to DocsListView.It ems.Count -1
    Dim li as ListViewItem = DocsListView.It ems(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

    Comment

    • Al Reid

      #3
      Re: ListView iteration order

      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" <larrylard@hotm ail.comwrote in message news:1152537042 .156706.112990@ b28g2000cwb.goo glegroups.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.It ems
      '...some code
      Next

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

      For i as Integer = 0 to DocsListView.It ems.Count -1
      Dim li as ListViewItem = DocsListView.It ems(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
      >

      Comment

      Working...