![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
For Each ... Next, Nested Array issues.
I am trying to perform a For Each Loop on a nested array and VB is yelling at me
The nested array structure is this. MainArray(0)
SubArray(0) = "Some String"
SubArray(1) = "Some String"
SubArray(2) = "Some String"
MainArray(1)
SubArray(0) = "Some String"
SubArray(1) = "Some String"
SubArray(2) = "Some String"Where SubArray always contains 3 total elements and MainArray can have any number of elements which are SubArray. Here is what I have. Private Sub ProcessArray(byref MainArray as Variant)
For Each SubArray In MainArrary
DoSomething SubArray(0), SubArray(1), SubArray(2)
Next
End SubFor Each control variable must be Variant or Object I've tried to declare SubArray before the loop like this. Dim SubArray(2) as Variant Dim SubArray() as Variant Can this just not be done in VB? Thanks in advance to anyone who posts. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Right, first of all if that's your code you'll get an ambiguous name error, and anyway it's a good idea to avoid keywords in the variable names. Also, VB is right, the control variable in the enumerator must be a variant.
Personally I would do it like this, which is more VB6 than .NET-ish Public MyArray(2, 2) As String
Public Sub Main()
Dim MyEnum As Integer
Dim MySubEnum As Integer
For MyEnum = LBound(MyArray, 1) To UBound(MyArray, 1)
For MySubEnum = LBound(MyArray, 2) To UBound(MyArray, 2)
Debug.Print MyArray(MyEnum, MySubEnum)
Next
Next
End Sub |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
For lack of confusion with my actual variable names I made bogus ones to more easily illustrate my methods.
Lately I've been doing a lot of PHP, and in OOP terms, PHP > VB. I wanted to avoid using that type of For Next loop in favor of a more effective For Each loop like in PHP, unfortunately I guess I can't I will post again about my implementation of this other method, thanks for your time. On a side note .NET = Evil. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|