Dynamic multidimensional arrays

Dynamic multidimensional arrays

Anonymous
Not applicable
1,349 Views
12 Replies
Message 1 of 13

Dynamic multidimensional arrays

Anonymous
Not applicable
Is there a way to make a array dynamic and multidimensional? What I've been doing is just setting the size at the start. Is there a way to add columns without having to predefine the size? or, after I'm done adding objects in the array, could I possibly just delete all of the blanks?

For Example --

Public TBAttr(1 To 10000, 1 To 5) As String

~Thanks for any help
-- Uriah
0 Likes
1,350 Views
12 Replies
Replies (12)
Message 2 of 13

dgorsman
Consultant
Consultant
You can only resize the last index of an array. So if you had (2,10) sized array you could change that to (2,x) but not (x,y). Check out Redim and Preserve.
----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 3 of 13

Anonymous
Not applicable
what about this:

option base 0
dim a as variant

a=array(0,1,2,3,4)
a(0)="first element"
a(1)=array("a","b","c")
a(2)=array(1,2)
a(3)=99
a(4)=array(0,1,2,3,4,5,6,7,8,9)

the idea is not to use *multidimensional* array m x n, but one-dimenzional array, whose elements may be anotjer arrays [or scalars, whatever] of any size you need, not all of them need to be the same size
0 Likes
Message 4 of 13

Anonymous
Not applicable
Thanks for the information . ~Cheers
0 Likes
Message 5 of 13

Anonymous
Not applicable
Well, its a nice idea, but it I don't want to seperate the values of the data. Thanks for your tip though ~Cheers
0 Likes
Message 6 of 13

Anonymous
Not applicable
better yet to use collection and then
convert it to multidimensional array
IMHO

~'J'~
0 Likes
Message 7 of 13

Anonymous
Not applicable
i am not sure, but you aren't separating anything

option base 0
dim a as variant
a=array(0)'you need it cose ubound() of empty arr throws err

'later in the code, when needed...
redim preserve a(ubound(a)+1)
a(ubound(a))=array(0,1,2,3,4)

'then populate the fourth elem
a(ubound(a))(3)=55

it is very easy to have arr grows as you need, no one elem left blank and unused
0 Likes
Message 8 of 13

Anonymous
Not applicable
LOL,
and you will be type 1000 times:
a(ubound(a))=array(1,2,3,4,5)
a(ubound(a))=array(6,7,8,9,10)
....
etc???

Good joke
Cheers 🙂

~'J'~
0 Likes
Message 9 of 13

Anonymous
Not applicable
sorry for misunderstanding

a=array(0,1,2,3,4)
means to dynamicaly allocate 5-elements array, just to have 5 placeholders, since "array()" function directly accepts *elements* not dimensions
so, you allocate 5 eleemtnts, and since it is variant, you put later whatever you want, even object pointer or anothe array

so to create functional resizeable array of 1000x5, you need:

a=array()
for i=0 to 999
redim preserve a(i)
a(i)=array(0,1,2,3,4)
next

or you may

for i=0 to 999
redim preserve a(i)
a(i)=array(empty,empty,empty,empty)
next

which gives you 1000x5 array of empties [not-initialized variant]
0 Likes
Message 10 of 13

jbooth
Advocate
Advocate
Now if you try to make it 1000x6 your program is going to slow down considerably (it already is slow so maybe the user won't notice).

Typically you should work with strong-typed arrays, and not variant datatypes. Not only is it faster, it is less prone to errors or accidental mistakes that are hard to debug and fix.

As Fatty already mentioned: if you only need to change the last dimension (ie: change 5x1000 to 5x1001) then use the redim statement. If you need to change to 6x1000 OR change to 5x1001 then write a utility function that will do it. Even if you have to declare a new array and copy each element it still may be faster than working with variant datatypes and late binding (with smaller arrays).
0 Likes
Message 11 of 13

Anonymous
Not applicable
just a moment. i am not going to tell that variant arrays [and variants at all] are better than fixed or strong typed.
just as idea that solution with variants is very flexible, when speed or memory is not the issue.

by the way, i mainly use vbscript [cscript particularly] to integrate all activex objects together into one neutral executing shell. cscript is the part of win and you have very quick solution, without need for compiling [again, of course if the execution speed is not the issue]
0 Likes
Message 12 of 13

Anonymous
Not applicable
I was thinking about the same thing, collections is more flexible than arrays are unless your in .net. Thanks
0 Likes
Message 13 of 13

Anonymous
Not applicable
well, I got it to work with Dim TestArray (1 To 10000, 5) . But I was wondering if I could change the dimensions during the process of adding in (after adding all information). Turns out, Mutidimensional arrays are a pain to redim and preserve. If I turned it into 6 parts, it wouldn't benifit me in really anyway. Because I have a compare sub that checks one array to another array. So thats alot of checking it has to do. Thanks for your input though 🙂

~Cheers
0 Likes