Re: Arrays



Hi Bob,

If you are careful, and the data are simple value types like double or long, then you can use copy memory to move the blocks around as a mutli dimensional array is just a continuous block of memory. You could then adjust the array dimensions via the safe array, but you are probably going to leak memory doing that.

alternatively you could use jagged arrays, although the support for them in VB6 is limited a bit, e.g:

Dim columns() As Variant
Dim row() As Long
Dim i As Long, j As Long

ReDim columns(0 To 5)
For i = 0 To 5
ReDim row(0 To 10)
columns(i) = row
Next i

For i = 0 To UBound(columns)
For j = 0 To UBound(columns(i))
columns(i)(j) = i * 10000 + j
Next j
Next i

For i = 0 To UBound(columns)
For j = 0 To UBound(columns(i))
Debug.Print i, j, columns(i)(j)
Next j
Next i

ReDim Preserve columns(0 To 3)
For i = 0 To UBound(columns)
row = columns(i)
ReDim Preserve row(0 To 5)
columns(i) = row
Next i

For i = 0 To UBound(columns)
For j = 0 To UBound(columns(i))
Debug.Print i, j, columns(i)(j)
Next j
Next i



Note that to re-dimension a row you need to copy it you, change it's size and copy it back in

The fastest alternative is likely to be to create a new array of the correct size and copy the data over to it.




"Bob" <someonw@xxxxxx> wrote in message news:%232eP%238OHJHA.3960@xxxxxxxxxxxxxxxxxxxxxxx
Hi Everyone:

I have a 2D array A(n,n). At times, I may need to decrease the size of the array by one, like

Redim B(0 to n-1,0 to n-1)
for i=1 to n
for j=1 to n
B(i-1,j-1)=A(i,j)
next j
next i

Is this the easiest and fastest way to achieve this, or is there a better way. Thanks for your help.

Bob


.



Relevant Pages


Loading