Assign different sized arrays

  • Thread starter Thread starter Wes
  • Start date Start date
W

Wes

Forgive the semi psuedo code.

Dim arr1(10), arr2(5)
arr1 is clear, arr2 is populated

Will this statement work?
arr1=arr2

If so, do arr1(6) to arr1(10) contain nulls?

Thxs
 
Na. Use dimension-less array and use only one variable per dim.


Dim arr1(10) AS long
Dim arr2() AS long

...
arr2=arr1


and the arr2 variable will get a COPY of arr"1:

arr1(5)=5
arr2=arr1
arr1(5)=55
Debug.Print arr1(5) = arr2(5)


if the arr2 was just as reference, not a copy, the last statement would be
true.



Vanderghast, Access MVP
 
Hi Wes

If you have already declared Arr1 as an array, then no, you cannot do this.
You will get a compile error: Can't assign to an array.

If you declared Arr1 as Variant and then used ReDim to make it an array,
then yes, you can assign Arr2 to it, but it will inherit the dimensions and
data type of Arr2.

So, if you declared Arr2(5) as Long, then Arr1 would become an array(5) of
Long.

You could then ReDim Preserve Arr1(10) but elements 6-10 would be 0, not
Null.

If Arr2 were declared as an array of Variant, then elements 6-10 would be
Empty, not Null.

The usual way to copy elements between incongruent arrays is to use a For
loop.
 
Back
Top