Solved

'ReDim PRESERVE' Multidimensional Array?

Posted on 1998-12-15
7
723 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

856 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