Trying to understand array size

I am trying to understand how XOJO handles array size as, from what I can see, all arrays seem to be unlimited irrespective of whether you try to limit their size.
If, for example, I run the example from the array documentation:

Var people() As String = Array(“John”, “Sally”, “Fred”, “Nancy”)
For Each name As String In People
MessageBox(name)
Next

but change it to, say:

Var people(2) As String = Array(“John”, “Sally”, “Fred”, “Nancy”)
For Each name As String In People
MessageBox(name)
Next

I still get exactly the same 4 name result even though in the 2nd case I would have expected to get only 2?

Can anyone please clarify?

Cheers
Olaf

arrays have no built in way to limit their size on creation like you’re expecting
for single dimension arrays you can always append and they grow dynamically

the only way you can to that is to use Redim or ResizeTo

this basically lets you remove elements beyond what ever maximum you want

What this does is allocate a 3-element array Var people(2) As String and then immediately throw it away and replace it with a 4-elelment array = Array("John", "Sally", "Fred", "Nancy")

See also our blog post about array sizes

https://www.mbsplugins.de/archive/2020-01-04/Array_size_allocation_in_Xojo