What's the proper way to Redim an array that is reused / appended to another?

Hi all, I have a double array of vertices I am appending to a variant array that represents the faces of a shape. The code looks like the following, I have to add several sets of vertice points to the variant array (but can’t do it in a loop because the points are variable) and was trying to reuse the same variable. But, I guess the append is by reference so all indexes of the variant array are always whatever the latest vertice array is. What’s the proper way to reuse a variable that is to be appended? I’ve done it before by actually using New and redifining instead of redim, but I bet there’s a better way.


Dim cube_holder() As Variant 
Dim vertices() As Double

//append top vertices
vertices.append LX0Z0_X //top top right
vertices.append H 
vertices.append LX0Z0_Z

break

cube_holder.append vertices //append vertice to cube array
Redim vertices(-1)

vertices.append X0Z0_X //top top left
vertices.append H
vertices.append X0Z0_Z


..etc 

Thanks!

Redim won’t work here. VERTICES still refers to the exact same array so when you redim you will redim the first one you appended to the cube_holder.

Thanks, I guess my question wasn’t quite complete (added another sentence) I’ve gotten around it before by just using New instead of redim, in those instances I’ve only need to do it once so didn’t really matter. It still doesn’t really matter, but what I mean to ask is what is the proper way to do what I am trying to do?

This should do it:

Dim cube_holder() As Variant
Dim vertices() As Double

 //top top right
vertices = MakeArray(LX0Z0_X, H, LX0Z0_Z)

break

cube_holder.append vertices //append vertice to cube array
//top top left
vertices = MakeArray( X0Z0_X,  H, X0Z0_Z  )

and add a method called MakeArray

Function MakeArray( p1 as souble, p2, as douuble, p3 as double) as Double()
      dim vertices() as double

      vertices.append p1
      vertices.append p2
      vertices.append p3

       return vertices

The way to see that you are not getting a new array on each redim is:

  1. Change the preference > debugging > show object ids.
  2. Debug run.
  3. After the second append if you put a break and examine the variant arrays contents each array has the same ID which means it’s the same object.

With the new code, you will see two different id’s in that variant.

Thanks Jason, I somehow felt that recreating the same array 15 times can’t be the best way … guess I am over thinking trivial things cause it’s getting late and I wish it were the weekend already!