vb6: redimensioning of 2D dynamic array

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 02:44:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!