0

I'm new to this, so sorry if my question has been asked before. I have searched but have not been able to find, or perhaps recognise, an answer. I am using visual studio 2008 and creating an app in vb.net.

I have 4 arrays named:- account1 account2 account3 account4. They all have 4 elements. I want to assign values to the elements in the arrays in an efficient manner. I thought two for next loops would do it. My pseudo code looks something like this

for i=1 to 4
    for c= 0 to 3
        account(i)(c)= 'mydata' /so account(i) would be account1 etc and c the element
    next c
next i

and thus all the elements of all the arrays are populated without me having to set up a fornext loop for each individual array name. How can I achieve this please.

Hope I have provided enough info to be of use, and that you can help. Thanks for all and any advice.

5
  • Can you format your code to make it more readable. Commented May 7, 2009 at 10:07
  • Do you want to assign the same values to each of the four arrays? Commented May 7, 2009 at 10:08
  • Yes, sorry for the lack of formatting for i= 1 to 4 for c = 0 to 3 account(i)(c)= some data next c next i so account(i) would be account1 account2 etc and (c) would be the individual elements of those arrays. The data for the arrays is taken from textboxes Hope this helps,and thank you for your interest Commented May 7, 2009 at 10:16
  • Eeeek how do I format for code? Commented May 7, 2009 at 10:17
  • There's a "code" button (1's & 0's) above the edit box. Either select your code and press that or indent your code lines by four spaces. Commented May 7, 2009 at 11:28

3 Answers 3

3

You should create a multidimensional array instead of 4 arrays, that would allow you to loop genericly through the arrays.

int[,] accounts = new int[4,4] // 4 accounts with 4 elements
for (int i = 0 ; i < accounts.GetUpperBound(0); i++)
  for (int j = 0 ; i < accounts.GetUpperBound(1); j++)
     accounts[i,j] = i*j;
  next
next
Sign up to request clarification or add additional context in comments.

2 Comments

That's a wired amalgamation of c# and vb.net you have created there...good answer though!
If you're wanting to do the same thing with an array of string arrays (that is, an array holding arrays of strings) to generate a csv file, how would you do that? That is, instead of GetUpperBound ?
2

If I understand correctly, why not:

For i as integer = 0 to 3
    account1(i) = "Account1"
    account2(i) = "Account2"
    account3(i) = "Account3"
    account4(i) = "Account4"
Next

Edit VB.Net for Qua's answer:

dim accounts(4,4) as integer

for i as integer = 0 To accounts.GetUpperBound(0)
  for j as integer = 0 To accounts.GetUpperBound(1)
     accounts(i, j) = new integer 'not needed for intergers, but if you had a class in here'
     accounts(i, j) = i*j;
  next
next

1 Comment

Thanks for all the contributions and sorry for my inability to format for code. All is ticketyboo with my app now, thanks again.
2

As I read your code example, I don't think that you need to use 2 seperate loops, as if I'm right you are assigning the same value to the ith position of your array eg:

array1(i) = array2(i) = array3(i) = array4(i)

In your example above you could write something like this (in pseudocode):

for i = 0 to 3
   account1(i) = MyData
   account2(i) = MyData
   account3(i) = MyData
   account4(i) = MyData
next i

I think that this is clearer than trying to write a loop for the variable names, especially for the number of arrays you are maintaining

Another option, which might be more appropriate if you have lots of arrays, would be to maintain a list of arrays which can then be iterated through simply enough.

PseudoCode for this option:

for each array in listOfArrays
  for i = 0 to 3
    array(i) = MyData
  next i
next

This is definitely clearer than trying to generate the names of the arrays dynamically, and more maintainable as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.