Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2005, 1:49 PM   #1
ChefBoiAreDee
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 ChefBoiAreDee is on a distinguished road
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"
...and so on.

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 Sub
Upon attempting to compile I get this error.

For Each control variable must be Variant or Object

I've tried to declare SubArray before the loop like this.
Dim SubArray(2) as Variant
and even like this.
Dim SubArray() as Variant
But I can't get anything to work

Can this just not be done in VB?

Thanks in advance to anyone who posts.
ChefBoiAreDee is offline   Reply With Quote
Old Mar 2nd, 2005, 4:19 PM   #2
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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
Hope this helps
Rory is offline   Reply With Quote
Old Mar 2nd, 2005, 4:26 PM   #3
ChefBoiAreDee
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 ChefBoiAreDee is on a distinguished road
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.
ChefBoiAreDee is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC