473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 21 '05 #1
5 1915
"B. Chernick" <BC*******@disc ussions.microso ft.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(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

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
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" <BC*******@disc ussions.microso ft.com> wrote in message
news:11******** *************** ***********@mic rosoft.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

Nov 21 '05 #3
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(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" <BC*******@disc ussions.microso ft.com> wrote in message
news:11******** *************** ***********@mic rosoft.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


Nov 21 '05 #4

"B. Chernick" <BC*******@disc ussions.microso ft.com> wrote in message
news:11******** *************** ***********@mic rosoft.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.
Nov 21 '05 #5
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/>

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
12333
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i;
8
1572
by: OZ | last post by:
how to run the index with 1, 2, 2,3,4,5..... in an array from a do loop ? TIA
1
879
by: Stephen R. G. Fraser | last post by:
Is this a bug in the for each loop or am I coding something wrong (or is this how its suppose to function and I don't understand how an array property and the for each interact The commented out code is the problem area as it generates the error: ->for each statement cannot operate on variables of type 'overloaded-function' using namespace System; ref class ArrayProp
13
1790
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include <stdlib.h> int resize(int *incsize, int min_size) { while(*incsize <= min_size) { *incsize = (int)(*incsize * 1.25); } if ( min_size > 60 ) return 0;
4
4146
by: outforblood74 | last post by:
Ok here's the deal, the for each statment using VB 6 doesn't loop all the objects. I would like it to close all the IE browsers that are open, but it doesn't work. And now I'm concerned that its doing this elsewhere in my code too, and the RETRY trick isn't an option. Starting with say 16 open IE windows, this doesn't work, it exits the for next while still having several IE windows open. >>>>>>>> Dim SWs As New SHDocVw.ShellWindows
7
1617
by: bearophileHUGS | last post by:
I have tried this, with Psyco it segfaults, and with Python 2.5 (on Win) hangs the interpreter, is it possible to improve the situation? class T(object): def __getattr__(self, x): dir(self) #import psyco #psyco.full() T().method() (Probably dir calls __getattr__).
10
5729
by: =?ISO-8859-1?Q?G=E9rard_Talbot?= | last post by:
www.authoring.stylesheets] Dear fellow CSS colleagues and web authors in alt.html discussion forum, I would like to ask you to help me confirm that there is a serious bug in IE 7 final release build 5730.11 under XP Pro SP2 with all the patches up-to-date. Please visit:
3
2445
by: werasm | last post by:
Hi all, I've been following the discussions concerning loops and whether to break or terminate mimicking the condition etc. I've had this recent case (which caused a bug) where I had to do something on checking the condition, whereafter exiting (the loop). It went something like this:
6
2268
by: Steve | last post by:
I have a function called myFunction that can be called once from an onclick() or 100 times from a function called loopFunction which contains a for(var i=0;i<100;i++) loop. Within myFunction is a setTimeout() function like this: setTimeout(function(){shuffleDoor()},1500); Now, when I call myFunction from onclick() it waits 1.5 seconds and then calls the shuffleDoor function, just as I imagine it should.
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.