Solved

'ReDim PRESERVE' Multidimensional Array?

Posted on 1998-12-15
7
730 Views
Last Modified: 2008-03-10
I read in the vb docs that when i use the PRESERVE keyword with multidimensional arrays that ONLY the last dimension is allowed to be altered. For example:
Create the Array:
Dim Test()
ReDim Test(0 to 10, 0 to 10, 0 to 10)
-------
Now Resize it:
ReDim PRESERVE Test(0 to 10, 0 to 10, 0 to 50)
This is CORRECT
--------
Now, here is an error:
ReDim PRESERVE Test(0 to 10, 0 to 50, 0 to 10)

Notice how the 2nd dimension was changed from "0 to 10" to "0 to 50" this is absolutely not allowed in VB. It is documented.

What i want to know is what a good workaround is so as to KEEP my same array but some how change that 2nd dimension size but still maintaining my content.

0
Comment
Question by:ljaques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450211
Why not change the order of what you are storing in the variant.

So instead of myvar(i,j,k) you access it like myvar(i,k,j) and now you can preserve everything and modify the last one.
0
 
LVL 3

Accepted Solution

by:
cognition earned 20 total points
ID: 1450212
A user defined type can contain a dynamic array. So you could use the following

Type MyTypeArray
    ArrayX()
    ArrayY()
    ArrayZ()
End Type

and then even hold the MyTypearray in another array.

Or you could use a class that has three arrays.
Or a collection that holds MyTypeArray's

Dim MyColection as new collection
MyCollection.add
0
 
LVL 15

Expert Comment

by:ameba
ID: 1450213
>a good workaround
VB is 'good' only with 1D arrays. Use 2 or more dimensions and forget speed and good memory handling.
To speed up redimensioning or copying array portions you can use VarPtr and APIs CopyMemory/ZeroMemory. Try searching Internet for samples and references, e.g. VBPJ has nice articles/samples.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ljaques
ID: 1450214
mirkwood,
  I forgot to mention....There are "two" dimensions that i want to varying their sizes not "one" as was demonstrated in my original example. So, if i tried to flip the order of my dimensions i would solve one's resizing problem but would be stuck with the other. ie. myvar(i,j,k) where j and k can vary in size.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450215
What do you expect it do when resizing two dimensions with keeping the data?


0
 
LVL 3

Expert Comment

by:cognition
ID: 1450216
I beleive you can do that with my example.

Redim Preserve MyArray.Array1(1 to 10)
Redim Preserve MyArray.Array2(1 to 20)
Redim Preserve MyArray.Array3(1 to 30)

Then

Redim Preserve MyArray.Array1(1 to 40)
Redim Preserve MyArray.Array2(1 to 50)
Redim Preserve MyArray.Array3(1 to 60)

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1450217
cognition: Your example stores 10+20+30 values = 60
While Test(1 to 10, 1 to 20, 1 to 30) = 10*20*30 values = 6000 values.

ljaques:
What do you expect it do when resizing two dimensions with keeping the data?  


 
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question