By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

Access arrays via for next loop

P: 5
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.
May 20 '09 #1
Share this Question
Share on Google+
3 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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.
May 20 '09 #2

P: 5
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.
May 20 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
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]
May 20 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.