Solved

'ReDim PRESERVE' Multidimensional Array?

Posted on 1998-12-15
7
702 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now