Home All Groups Group Topic Archive Search About

My for each loops do not compile - VB 2003

Author
11 Aug 2006 4:47 PM
IanT
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

Author
11 Aug 2006 5:11 PM
pvdg42
Show quote Hide quote
"IanT" <I***@discussions.microsoft.com> wrote in message
news:62E63E63-FC2F-444C-BE0B-9EA0AADF53EB@microsoft.com...
>
> 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)?
Author
11 Aug 2006 5:13 PM
crazyone
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
Author
11 Aug 2006 6:50 PM
AMDRIT
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


Show quoteHide quote
"crazyone" <theinsaneco***@gmail.com> wrote in message
news:1155316425.980011.15790@i3g2000cwc.googlegroups.com...
> 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
>
Author
11 Aug 2006 7:07 PM
IanT
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.

Show quoteHide quote
"crazyone" <theinsaneco***@gmail.com> wrote in message news:1155316425.980011.15790@i3g2000cwc.googlegroups.com...
> 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
>
Author
14 Aug 2006 12:08 AM
GhostInAK
Hello IanT,

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

-Boo

Show quoteHide quote
> 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