Solved

# ReDim(ing) a multidimensional array

Posted on 2005-04-26
Medium Priority
1,619 Views
how do you ReDim a multidimensional array?

Can you ReDim a multidimensional array
0
Question by:shuittny
• 3
• 3
• 2

LVL 7

Expert Comment

ID: 13870335
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

LVL 7

Expert Comment

ID: 13870347
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 Comment

ID: 13878766
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

LVL 96

Accepted Solution

Bob Learned earned 500 total points
ID: 13879081
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

LVL 7

Expert Comment

ID: 13879093
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 Comment

ID: 13911876
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

LVL 96

Expert Comment

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

Bob
0

Author Comment

ID: 13930396
Bob,

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
###### Suggested Courses
Course of the Month17 days, 12 hours left to enroll