Link to home
Start Free TrialLog in
Avatar of shuittny
shuittny

asked on

ReDim(ing) a multidimensional array

how do you ReDim a multidimensional array?

Can you ReDim a multidimensional array
Avatar of brdrok
brdrok

uhm....

according to the msdn...I believe you can.

ReDim MyArray(10, 20)
or if you like to preseve the existing values use the Preseve keyword

ReDim Preserve MyArray(10, 20)
as I kept on reading...i probably should note that msdn also says:

"you can change only the last dimension when you use Preserve"
Avatar of shuittny

ASKER

hmmmm,

       my next question was going to be could you create a dynamic multidimensional array

       myArray(,)
       for i = 0 to 50
          redim myArray(i,1)
          myArray(i,1) = i
       next
................

but based on your response I guess that can't happen (and get the desired result)

Sean
         
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well....alot of the answers i get from asking a questions starts with "It depends" =)

it depends on what you are trying to accomplish.  one thing i did notice in your code snippet is that you redim your array inside a loop.

I think it would be best to redim your array outside of the loop.  

the following code is from a windows application (vb.net) but principle ought to remain the same regardless whether it's asp.net or a windows app.

dim myArray(1, 2) as string
dim output as string
dim c as integer
dim r as initeger

redim myArray(5, 2)  'you changed your mind and want to increase the array size

for r = 0 to 5
   for c = 0 to 2
     myArray(r, c) = r.ToString() + ", " + c.ToString()
     output += myArray(r, c) + vbcrlf
    next
next

TextBox1.Text = output

the only issue is that I won't exactly know the size of the dynamic array.  The user would determine the size

Invision a form that contains a listbox, that listbox contains (could...) mulitple values.  So on one entry somebody can select 5 items from the list, the next 0 or 1 or 10, etc.

Now I want to create an array (multi-dimensional) that can hold that value, but dynamically.  I mean if the listbox (for example) held a max value of 15 values. I could do a

dim myArray(15, 1) as string

except in most cases the user won't select 15, which means I'm wasting space.  Now I can obtain these values (...and the listbox is only an example, the first one that comes to mind) via a loop and embed an counter, like so:

       myArray(,)
       for i = 0 to 50
          redim myArray(i,1)
          myArray(i,1) = i
       next

this way I'm always adding at least one element to the array, get a precise amount!  I'm not wasting space and I'm not creating multiple loops knowing that I can knock this down in a few lines, which saves time and probably performance
Did you read my post about jagged arrays?  You can dimension each array element separately, saving space.

Bob
Bob,

      I took your example and tweaked it just a bit.  Now it works perfectly!