Reinitializing an array

let’s say you have an array called Names(5).

If you redim the array, will it delete the existing data in the array?

If not, what is the best way to empty out the array, Redim Names(-1)?

No.

Yes

I guess “best” way depends on what you want to do. You could also do

for i As integer = 0 to Ubound(myArray) myArray(i) = "" //for a string array next i
and now you have your original array with empty elements.
Redim myArray(-1) is certainly easier

That’s what I was thinking. I didn’t know if there was a magic Array.Clear method or something, though.

If you want to clear all the values in an array, that is, reset all the elements as if it were a new array, the fastest way is probably this:

dim ub as integer = myArr.Ubound
redim myArr( -1)
redim myArr( ub )

From 2019r2 use:

anArray.ResizeTo(-1)

see: https://documentation.xojo.com/api/language/arrays.html#arrays-resizeto

Or

myArr.RemoveAllRows

1 Like

@Kem_Tekinay, more readable, great !

If you redim it to a smaller value, the data past the new end will be destroyed. The data within the new bounds will be preserved.