(01-06-2023, 03:48 PM)Spriggsy Wrote: Pretty sure PRESERVE works with multiple dimension arrays. I think my SQL stuff that depends on a two-dimensional array wouldn't work if PRESERVE only handled one dimension arrays.
It works, but only on one dimension.
REDIM x(10,10)
REDIM x(10,20) <-- this is fine.
REDIM x(20,10) <-- this will corrupt your index.
The reason is real simple -- redim just copies the existing mem block and moves it to a new memblock, and data is stored in memory as a 1d block of memory.
For example:
12
34
56
78
The above is a 2 x 4 array of single digits. In memory, it's stored simply as "12345678". Left to right, top to bottom.
Now, redim and add a column, you get:
123
456
780
000
Corrupted data! Sure, you added 4 bytes to the whole array, but when you moved that "12345678" over with those 4 new "0000" that you just created, they map to the index in the manner above.
So, why can you create a new row with no problems??
12
34
56
78
00
See where your "12345678" copied over and remained in the same index positions? Those two new "0" characters are at the end of the data, but the structure of the data itself hasn't changed. We still read it left-to-right, top-to-bottom.
Multi-dimensional arrays are only redim _preserve-able with the last element. Anything else, you need to move manually into a new array yourself.