503 views

# Arrays - Dim, Redim, Redim(-1)

1. 7 months ago

### Gary T

13 Sep 2018

Let me apologize up front for a very basic question, but after reading the documentation and several posts on the forum, I'm not sure I understand how this works.

The documentation says...

The Redim command can be used to increase or decrease the number of elements in a previously declared array. Unlike with Dim, the various upper bound values can be Integer literals, constants, variables or expressions.

I think I'm good with this. Redim changes the dimensions of an array you've already dimentioned

When you Redim an array to an unbounded size (using -1 as the upperBound), then all its elements are removed. It is faster to use Redim in this manner to clear any array rather than looping through the array and calling the Remove method on each element.

Redim(-1) alone removes all elements of the array? I've read elsewhere on the forum that it only makes the array "unbounded" and the data, if any, from the previous dimensioned use is retained until you Redim(positiveValue). So, which clears all the values, redim anArray(-1), or Redim anArray(-1) then Redim anArray(X)?

Second question. What is the meaning of an unbounded array? Does this mean that after Redim anArray(-1) , anArray(N) = X is valid for all N, assuming memory allows? If this is true, is there a performance penalty for using unbounded arrays?

When you increase the size of an array, any existing values are retained.

Is this true for enlarging any dimension of the array? For example, Dim anArray(10,20,30) followed by Redim anArray(11,22,31) retains all data from the previous dimensioning? What data, if any, resides in the newly dimensioned space?

When you decrease the size of an array, existing values are retained if they are still within the new array bounds.

Same question as increasing dimension size. Does this retain the data when reducing the size of any dimension in a multidimensional array?

Sorry again for this very basic question (ok, questions plural), but the more I researched, the more confused I became, and by not understanding this, I envision hours of pain trying to figure out why things work sometimes and not others and fighting the dreaded "out of bounds" error.

Thanks,

Gary

See post in context

@Gary T Redim(-1) alone removes all elements of the array? I've read elsewhere on the forum that it only makes the array "unbounded" and the data, if any, from the previous dimensioned use is retained until you Redim(positiveValue).

No, the data is disposed of immediately.

@Gary T What is the meaning of an unbounded array? Does this mean that after Redim anArray(-1) , anArray(N) = X is valid for all N, assuming memory allows?

No. Unbounded means has no data. No bounds means the array is completely empty, there is no storage allocated for it until you give it bounds.

@Gary T For example, Dim anArray(10,20,30) followed by Redim anArray(11,22,31) retains all data from the previous dimensioning?

Yes, all the data is retained.

@Gary T What data, if any, resides in the newly dimensioned space?

The default value for the data type of the array. Zero for integers, "" for strings, Nil for objects, etc.

@Gary T Same question as increasing dimension size. Does this retain the data when reducing the size of any dimension in a multidimensional array?

Yes, the values of the remaining cells of the array retain their values.

2. ### Dave S

13 Sep 2018 San Diego, California USA

Redim x(-1), removes all current contents, and basically creates an "empty" array, with no pre-allocated storage. you can then APPEND items to it as required

As to the other two questions.... honestly those I can't answer as I have never in my entire life needed to do either of those operations... but I might suggest one way to verify it.... is to TRY IT

3. ### Tim H

13 Sep 2018 Pre-Release Testers Answer Portland, OR USA

@Gary T Redim(-1) alone removes all elements of the array? I've read elsewhere on the forum that it only makes the array "unbounded" and the data, if any, from the previous dimensioned use is retained until you Redim(positiveValue).

No, the data is disposed of immediately.

@Gary T What is the meaning of an unbounded array? Does this mean that after Redim anArray(-1) , anArray(N) = X is valid for all N, assuming memory allows?

No. Unbounded means has no data. No bounds means the array is completely empty, there is no storage allocated for it until you give it bounds.

@Gary T For example, Dim anArray(10,20,30) followed by Redim anArray(11,22,31) retains all data from the previous dimensioning?

Yes, all the data is retained.

@Gary T What data, if any, resides in the newly dimensioned space?

The default value for the data type of the array. Zero for integers, "" for strings, Nil for objects, etc.

@Gary T Same question as increasing dimension size. Does this retain the data when reducing the size of any dimension in a multidimensional array?

Yes, the values of the remaining cells of the array retain their values.

4. ### Gary T

14 Sep 2018

Thank you guys. I really appreciate your responses. While I have your attention, a follow-on question if I may...

It's my understanding the principal (or at least significant) use of Redim is in cases where the quantity of data is unknown. Such as reading rows from a user specified file. In that case, we might have...

Dim anArray (1000) as string

While not file.EOF
anArray(N) = filerow
if N >= anArray.ubound - 5 then Redim anArray(anArray.ubound + 10) // Got close, so ask for 10 more slots.
N = N + 1
loop

Am I on solid ground with this?

Thanks again!

Gary

5. ### Dave S

14 Sep 2018 San Diego, California USA
Edited 7 months ago
```Dim anArray (-1) as string

While not file.EOF
anArray.append filerow
loop```

no need to keep "expanding" as you read... let the app do that for you....

6. ### Jason P

14 Sep 2018 Xojo Inc Texas
Edited 7 months ago

Dave is correct. Append will automatically grow the array and add the item to the array at "the end”. For multi-dimensional arrays, redim is useful.

7. ### Gary T

14 Sep 2018

Ah, ok. Thanks Dave. I also understand now why you don't encounter the results of resizing with Redim. I guess the principal difference is that Append works on one dimensional arrays (the most common in my use), which Redim can work on arrays with any number of dimensions.

Thank you all.

```s=txtfile.readall // assuming you have a textinputstream open