问题
I am using arrays to store properties of steam according to it's pressure. Right now I have properties of exactly 9 pressures so I'm using static array. I'd like to be more flexible so I'd like to switch to dynamic arrays.
When I use ReDim foo(1 to i, 1 to 10)
in loop I completely loose all data except last line.
When I use ReDim Preserve foo(1 to i, 1 to 10)
or ReDim Preserve(i,10)
Program throws error of "Runtime error '9': subscript out of range". i
goes from 1 to 9.
How can I add line/column to array full of data without loosing them?
回答1:
You may only Redim Preserve the final dimension in a VB6 multidimensional array. Here's the info from MSDN:
If you include the Preserve keyword, Visual Basic copies the elements from the existing array to the new array. When you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same size it already has in the existing array.
For example, if your array has only one dimension, you can resize that dimension and still preserve the contents of the array, because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension if you use Preserve.
The following example increases the size of the last dimension of a dynamic array without losing any existing data in the array, and then decreases the size with partial data loss:
Dim IntArray(10, 10, 10) As Integer
ReDim Preserve IntArray(10, 10, 20)
ReDim Preserve IntArray(10, 10, 15)
来源:https://stackoverflow.com/questions/6276761/vb6-redimensioning-of-2d-dynamic-array