For Each loop problem - (Bug?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B. Chernick

    #1

    For Each loop problem - (Bug?)

    Is this a bug or just bad programming? I've never encountered this problem
    before.
    (Bare minimum sample form to illustrate.) I've also tried this with a
    class. Same result.
    The error is: For loop control variable 'd' already in use by an enclosing
    For loop.
    -----------------------------------------
    Structure docdefs
    Public c As Collection
    Public d As Integer
    End Structure

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Dim a As docdefs
    Dim b As docdefs

    For Each a.d In a.c
    For Each b.d In b.c
    ' some code here
    Next
    Next

    End Sub

  • Herfried K. Wagner [MVP]

    #2
    Re: For Each loop problem - (Bug?)

    "B. Chernick" <BChernick@disc ussions.microso ft.com> schrieb:[color=blue]
    > Is this a bug or just bad programming? I've never encountered this
    > problem
    > before.
    > (Bare minimum sample form to illustrate.) I've also tried this with a
    > class. Same result.
    > The error is: For loop control variable 'd' already in use by an
    > enclosing
    > For loop.
    > -----------------------------------------
    > Structure docdefs
    > Public c As Collection
    > Public d As Integer
    > End Structure
    >
    > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    > System.EventArg s) Handles MyBase.Load
    > Dim a As docdefs
    > Dim b As docdefs
    >
    > For Each a.d In a.c
    > For Each b.d In b.c
    > ' some code here
    > Next
    > Next[/color]


    I consider this a bug because it's clear that the same variable is not being
    reused. I'll investigate...

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Phil G.

      #3
      Re: For Each loop problem - (Bug?)

      I'm sure someone else will post why but one solution would be :

      Structure docdefs
      Public c As Collection
      Public d As Integer
      End Structure

      Structure docdefs1
      Public c As Collection
      Public d As Integer
      End Structure

      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      Dim a As New docdefs
      Dim b As New docdefs1

      For Each a.d In a.c
      For Each b.d In b.c
      ' some code here
      Next
      Next

      End Sub

      Rgds, Phil

      "B. Chernick" <BChernick@disc ussions.microso ft.com> wrote in message
      news:115D8BC6-03E5-4F75-BADA-07C5D1EE5571@mi crosoft.com...[color=blue]
      > Is this a bug or just bad programming? I've never encountered this
      > problem
      > before.
      > (Bare minimum sample form to illustrate.) I've also tried this with a
      > class. Same result.
      > The error is: For loop control variable 'd' already in use by an
      > enclosing
      > For loop.
      > -----------------------------------------
      > Structure docdefs
      > Public c As Collection
      > Public d As Integer
      > End Structure
      >
      > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      > System.EventArg s) Handles MyBase.Load
      > Dim a As docdefs
      > Dim b As docdefs
      >
      > For Each a.d In a.c
      > For Each b.d In b.c
      > ' some code here
      > Next
      > Next
      >
      > End Sub
      >[/color]


      Comment

      • B. Chernick

        #4
        Re: For Each loop problem - (Bug?)

        I suppose so but the whole purpose of my code was to combine all related
        items in one structure definition. This sort of defeats the whole purpose.

        "Phil G." wrote:
        [color=blue]
        > I'm sure someone else will post why but one solution would be :
        >
        > Structure docdefs
        > Public c As Collection
        > Public d As Integer
        > End Structure
        >
        > Structure docdefs1
        > Public c As Collection
        > Public d As Integer
        > End Structure
        >
        > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
        > System.EventArg s) Handles MyBase.Load
        > Dim a As New docdefs
        > Dim b As New docdefs1
        >
        > For Each a.d In a.c
        > For Each b.d In b.c
        > ' some code here
        > Next
        > Next
        >
        > End Sub
        >
        > Rgds, Phil
        >
        > "B. Chernick" <BChernick@disc ussions.microso ft.com> wrote in message
        > news:115D8BC6-03E5-4F75-BADA-07C5D1EE5571@mi crosoft.com...[color=green]
        > > Is this a bug or just bad programming? I've never encountered this
        > > problem
        > > before.
        > > (Bare minimum sample form to illustrate.) I've also tried this with a
        > > class. Same result.
        > > The error is: For loop control variable 'd' already in use by an
        > > enclosing
        > > For loop.
        > > -----------------------------------------
        > > Structure docdefs
        > > Public c As Collection
        > > Public d As Integer
        > > End Structure
        > >
        > > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
        > > System.EventArg s) Handles MyBase.Load
        > > Dim a As docdefs
        > > Dim b As docdefs
        > >
        > > For Each a.d In a.c
        > > For Each b.d In b.c
        > > ' some code here
        > > Next
        > > Next
        > >
        > > End Sub
        > >[/color]
        >
        >
        >[/color]

        Comment

        • _AnonCoward

          #5
          Re: For Each loop problem - (Bug?)


          "B. Chernick" <BChernick@disc ussions.microso ft.com> wrote in message
          news:115D8BC6-03E5-4F75-BADA-07C5D1EE5571@mi crosoft.com...
          :
          : Is this a bug or just bad programming? I've never encountered this
          : problem before.
          : (Bare minimum sample form to illustrate.) I've also tried this with a
          : class. Same result.
          : The error is: For loop control variable 'd' already in use by an
          : enclosing For loop.
          : -----------------------------------------
          : Structure docdefs
          : Public c As Collection
          : Public d As Integer
          : End Structure
          :
          : Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
          : System.EventArg s) Handles MyBase.Load
          : Dim a As docdefs
          : Dim b As docdefs
          :
          : For Each a.d In a.c
          : For Each b.d In b.c
          : ' some code here
          : Next
          : Next
          :
          : End Sub


          While this may be a bug, I think the actual root problem is that your
          are using a member variable of the docdefs structure as your enumerator
          (don't know why that would be a problem mind you, but there it is). I
          don't know if this will do what you're looking for but how about the
          following?


          ---------------------------------------------------------
          Private Sub Form1_Load(ByVa l sender As System.Object, _
          ByVal e As System.EventArg s) Handles MyBase.Load
          Dim a As New docdefs
          Dim b As New docdefs

          'since members in a structure can't be declared 'New', you'll
          'have to manually instantiate them here
          a.c = New Collection
          b.c = New Collection

          'Use a dedicated enumerator for each loop rather than the
          'member variable a.d / b.d
          For Each d1 As Integer In a.c
          For Each d2 As Integer In b.c
          ' some code here
          Next
          Next

          End Sub

          Structure docdefs
          Public c As Collection

          'this isn't necessary unless there is another purpose not
          'disclosed in the original posting.
          'Public d As Integer
          End Structure
          ---------------------------------------------------------



          Note, this won't work unless the member collection 'c' in docdefs only
          contains integer values (or strings that can beconverted to iteger).
          For example, the following will compile and run:


          ---------------------------------------------------------
          Dim a As New docdefs
          Dim b As New docdefs
          a.c = New Collection
          b.c = New Collection

          a.c.add(1)
          a.c.add("2")
          b.c.add(3)
          b.c.add("4")

          For Each d1 As Integer In a.c
          Console.WriteLi ne("d1: " & d1)

          For Each d2 As Integer In b.c
          Console.WriteLi ne(vbTab & "d2: " & d2)

          Next
          Next
          ---------------------------------------------------------


          This will produce the output:

          ---------------------------------------------------------
          d1: 1
          d2: 3
          d2: 4
          d1: 2
          d2: 3
          d2: 4
          ---------------------------------------------------------


          However, the following will thrown an exception:


          ---------------------------------------------------------
          Dim a As New docdefs
          Dim b As New docdefs
          a.c = New Collection
          b.c = New Collection

          a.c.add(1)
          a.c.add("2")
          b.c.add(3)
          b.c.add("A") '<- offending line

          Try
          For Each d1 As Integer In a.c
          Console.WriteLi ne("d1: " & d1)

          For Each d2 As Integer In b.c
          Console.WriteLi ne(vbTab & "d2: " & d2)

          Next
          Next
          Catch e As Exception
          Console.WriteLi ne(e.message)
          End Try
          ---------------------------------------------------------


          This generates the following output:


          ---------------------------------------------------------
          d1: 1
          d2: 3
          Cast from string "A" to type 'Integer' is not valid.
          ---------------------------------------------------------


          HTH


          Ralf
          --
          ----------------------------------------------------------
          * ^~^ ^~^ *
          * _ {~ ~} {~ ~} _ *
          * /_``>*< >*<''_\ *
          * (\--_)++) (++(_--/) *
          ----------------------------------------------------------
          There are no advanced students in Aikido - there are only
          competent beginners. There are no advanced techniques -
          only the correct application of basic principles.


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: For Each loop problem - (Bug?)

            Addendum:

            Bug report:

            <URL:http://lab.msdn.micros oft.com/productfeedback/viewfeedback.as px?feedbackid=a b8da617-37fd-4431-a0e3-75c70e94f1c1>

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            Working...