Solved

'ReDim PRESERVE' Multidimensional Array?

Posted on 1998-12-15
7
712 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now