For Each loop problem - (Bug?)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
B. Chernick said:
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


I consider this a bug because it's clear that the same variable is not being
reused. I'll investigate...
 
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
 
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.
 
:
: 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
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Loop not behaving as expected 13
Debugging Error 1
Latebound Exception? 3
Bindingsource problem 3
Finding IP from vb.net 1
Do Loops and Timers 13
StreamReader loop, please help 1
invalid time - clock adjusted forward 19

Back
Top