Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

'ReDim PRESERVE' Multidimensional Array?

Posted on 1998-12-15
7
Medium Priority
?
771 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 80 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
Industry Leaders: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

926 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