I know about the limitation on only being able to change the upper bound of the last array dimension, but I have a need to be able to change dynamically both bounds of a 2-dimensional Array using Preserve. Is there another way to accomplish this?
Subject: Determine size before loading array
Can you analyze the data and redim the array to the size needed before you actually enter data into the array? My understanding is that the redim command is “expensive” so repeated redimming of an array is not recommended. (You can redim all dimensions of an array if you don’t use the preserve argument.)
Subject: ReDim Preserve 2-dimensional Array
A two-dimensional array is really just a one-dimensional array with its index divided into two parts. To figure out where a particular element(x, y) is stored, the formula is y * n + x, where n is the number of elements in the x dimension.
That’s why you use can Preserve and still change the number of elements in the y dimension, because the formula for finding an element doesn’t change. But if you change the number of elements in the x dimension, you’re changing the value of n. So all the elements on row 2 thru whatever would have to be shifted accordingly. Preserve doesn’t shift things. It just redefines the size of what you already have.
If you want to write the code to shift things when you redimension, you can do that. Define a class named Array, which contains a one-dimensional array, and when you get two coordinates the class will figure out what one-dimensional index that corresponds to. You can provide a method to resize, which moves the values in your linear array to their new positions.