# ReDim(ing) a multidimensional array

how do you ReDim a multidimensional array?

Can you ReDim a multidimensional array
###### Who is Participating?

Commented:
Jagged arrays:

Dim n()() As Integer

ReDim Preserve n(3)

n(0) = New Integer() {5, 3, 4}
n(1) = New Integer() {9, 3, 4, 10}
n(2) = New Integer() {1, 2, 3, 4}

ReDim Preserve n(2)(4)

n(2)(4) = 10

Console.WriteLine(n(2)(4)) ' <---- 10

Bob
0

Commented:
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)
0

Commented:
as I kept on reading...i probably should note that msdn also says:

"you can change only the last dimension when you use Preserve"
0

Author Commented:
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

0

Commented:
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

0

Author Commented:
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
0

Commented:
Did you read my post about jagged arrays?  You can dimension each array element separately, saving space.

Bob
0

Author Commented:
Bob,

I took your example and tweaked it just a bit.  Now it works perfectly!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.