Connecting Tech Pros Worldwide Forums | Help | Site Map

Access arrays via for next loop

Newbie
 
Join Date: May 2009
Posts: 5
#1: May 20 '09
I have 5 arrays, named array1 array2 array3 array4 array5. They are integer arrays each with 5 elements in them. I want to access all the elements in all the arrays using a for next loop,the pseudo code goes something like this:

Expand|Select|Wrap|Line Numbers
  1. for i = 1 to 5  'this loop calls each array
  2.  for j = 0 to 4 ' this loop calls each element
  3.      if array (i,j) then 
  4.         ' code goes here 
  5.     end if
  6.  next j
  7. next i
  8.  
  9.  
How would i do that please? Again, thanks for all and any help.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,782
#2: May 20 '09

re: Access arrays via for next loop


You seem to have a handle on it. What part exactly are you having a problem with? Is it that you don't know the actual syntax for a 'for loop' in VB?

Welcome to learning a programming language! It's always good to have people join the ranks. You will find a LOT of introductory 'how to' websites by doing a little googling. But your most valuable resource for Microsoft languages such as VB or C# would be the MSDN itself.
Newbie
 
Join Date: May 2009
Posts: 5
#3: May 20 '09

re: Access arrays via for next loop


Hi and thanks for your reply. My struggle is with the syntax in this line of pseudo code

Expand|Select|Wrap|Line Numbers
  1.  
  2. if array (i,j) 
  3.  
  4.  
Instead of having to name each array directly in the loop, I want the loop counter to be used so that the above line is saying at the first pass of the i loop,
Expand|Select|Wrap|Line Numbers
  1. if array1 , element j
  2.  
then on each subsequent pass of the i loop the array in question becomes
array2 array3 array4 array5 . Thats the bit I'm not sure about. Hope I've explained myself clearly and if you can help then many thanks.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,782
#4: May 20 '09

re: Access arrays via for next loop


if you have five different arrays, then the nested loop approach you have is how you do it.

If you have a single array, five elements wide, then you could do it with a couple index counters

ArrayName[Index1, Index2, Index3, Index4, Index5]
Reply


Similar Visual Basic .NET bytes