:
: 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
--