My for each loops do not compile - VB 2003

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

Guest

Public Class MyForEachBug
'
Public Class MyData
Public Element As String
Public Group() As String = {"a", "b", "c"}
End Class
'
Public Sub New()
'
Dim Element As String
Dim Group() As String = {"a", "b", "c"}
Dim D1 As New MyData
Dim D2 As New MyData
'
'
For Each Element In Group
For Each D1.Element In D1.Group
For Each D2.Element In D2.Group
Console.WriteLine _
( _
Element & D1.Element & D2.Element _
)
Next
Next
Next
End Sub
'
End Class
 
IanT said:
Public Class MyForEachBug
'
Public Class MyData
Public Element As String
Public Group() As String = {"a", "b", "c"}
End Class
'
Public Sub New()
'
Dim Element As String
Dim Group() As String = {"a", "b", "c"}
Dim D1 As New MyData
Dim D2 As New MyData
'
'
For Each Element In Group
For Each D1.Element In D1.Group
For Each D2.Element In D2.Group
Console.WriteLine _
( _
Element & D1.Element & D2.Element _
)
Next
Next
Next
End Sub
'
End Class
Could you share exactly what compiler error(s) you got and at which line(s)?
 
In my compiler it says "For loop control variable Element already in
use by an enclosing For Loop.

I think it's a bug in the compiler because it doesn't take into account
the fact that Element is part of D2 and not D1...

To Change this, you'll have to declare a second variable called
differently and use that one instead of D2.Element.

Math
 
Public Class MyForEachBug
'
Public Class MyData
Public Element As String
Public Group() As String = {"a", "b", "c"}
End Class
'
Public Sub New()
'
Dim Element As String
Dim Group() As String = {"a", "b", "c"}
Dim D1 As New MyData
Dim D2 As New MyData
'
'
For Each Element In Group
For Each D1.Element In D1.Group
For Each D2.Element In D2.Group
Console.WriteLine _
( _
Element & D1.Element & D2.Element _
)
Next
Next
Next
End Sub
'
End Class


1. D1 is not instantiated
2. D2 is not instantiated
3. What is that that you are attempting to do?
a. Find every occurance of an item in Group, in d1.group and then in
d2.group?
b. Find every combination of the occurances in group, dr.group,
d2.group?

Here is an approach for a:

d1 = new myData: d1.element = string.empty: d1.group = new
string(){"a","B","c"}
d2 = new myData: d2.element = string.empty: d2.group = new
string(){"A","b","C"}

'Find the first index
For Each Element In Group

'Find the second index that matches the first index
For Each Element1 as string In D1.Group

If String.Compare(Element, Element1) = 0 Then

'Find the third index that matches the second index
For Each Element2 as string In D2.Group

If String.Compare(Element1, Element2) = 0 Then

'Inform us that the indexes has been found
Console.WriteLine(String.Format("Element 1:{0}, 2:{1}, 3:{0}",
Element, Element1, Element2))

Exit For

End If

Next

Exit For

End If

Next

Next
 
Yes that's the error I get.
And I agree, its a compiler short circuit.

If the VB really did not support dot names then
"D1.Element" should fail because "Element" was already used.
 
Hello IanT,

usually you declare your loop index variables outside of the class. You
declare them where you are going to use them.

-Boo
 
Back
Top