Connecting Tech Pros Worldwide Forums | Help | Site Map

For Each loop problem - (Bug?)

B. Chernick
Guest
 
Posts: n/a
#1: Nov 21 '05
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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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]
Guest
 
Posts: n/a
#2: Nov 21 '05

re: For Each loop problem - (Bug?)


"B. Chernick" <BChernick@discussions.microsoft.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(ByVal sender As System.Object, ByVal e As
> System.EventArgs) 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/>

Phil G.
Guest
 
Posts: n/a
#3: Nov 21 '05

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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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@discussions.microsoft.com> wrote in message
news:115D8BC6-03E5-4F75-BADA-07C5D1EE5571@microsoft.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(ByVal sender As System.Object, ByVal e As
> System.EventArgs) 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]


B. Chernick
Guest
 
Posts: n/a
#4: Nov 21 '05

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(ByVal sender As System.Object, ByVal e As
> System.EventArgs) 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@discussions.microsoft.com> wrote in message
> news:115D8BC6-03E5-4F75-BADA-07C5D1EE5571@microsoft.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(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) 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]
_AnonCoward
Guest
 
Posts: n/a
#5: Nov 21 '05

re: For Each loop problem - (Bug?)



"B. Chernick" <BChernick@discussions.microsoft.com> wrote in message
news:115D8BC6-03E5-4F75-BADA-07C5D1EE5571@microsoft.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(ByVal sender As System.Object, ByVal e As
: System.EventArgs) 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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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.WriteLine("d1: " & d1)

For Each d2 As Integer In b.c
Console.WriteLine(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.WriteLine("d1: " & d1)

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

Next
Next
Catch e As Exception
Console.WriteLine(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.


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Nov 21 '05

re: For Each loop problem - (Bug?)


Addendum:

Bug report:

<URL:http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=ab8da617-37fd-4431-a0e3-75c70e94f1c1>

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

Closed Thread