|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
My for each loops do not compile - VB 2003' 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
Show quote
Hide quote
"IanT" <I***@discussions.microsoft.com> wrote in message Could you share exactly what compiler error(s) you got and at which line(s)?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 > 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 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 > 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 > 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
Can I make my program more efficient ?
Can anyone help in shortening this loop??? Event handlers tcpListener.AcceptSocket strange behavior DataGridView Column Alignment Trouble with pixels Quesion about ADO.NET in VB.NET Form2.vb[design] innaccessible ! Binding an Image to a picturebox Tip of The Day |
|||||||||||||||||||||||