By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,569 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

For Each loop problem - (Bug?)

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"B. Chernick" <BC*******@discussions.microsoft.com> schrieb:
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...

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

Nov 21 '05 #2

P: n/a
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" <BC*******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.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

Nov 21 '05 #3

P: n/a
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:
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" <BC*******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.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


Nov 21 '05 #4

P: n/a

"B. Chernick" <BC*******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.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.
Nov 21 '05 #5

P: n/a
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/>

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.