Declare array with number?


I try to declare a byte array this way:

Dim encoded(frame.Size - startbyte-1) As Byte

frame.Size and startbyte are both integers.

The error I encounter is:

I can’t do anything with a constant here, because I cannot declare it from integers. And I never heard of the number datatype (cannot find it in the docs either).
I tried this:

Dim encoded(-1) As Byte

But I encounter a OutOfBoundException, I don’t know why, because I thought -1 meant variable length.

Can someone help me with what I try to do?


Dim encoded() As Byte ReDim encoded(frame.Size - startbyte-1)

All arrays in Xojo are of variable length.
Dim arr(-1) As Byte means the same as Dim arr() As Byte. It is an empty array.
Resize an array – as Ramon mentioned above – with ReDim.
As all arrays are of variable length, you can always use Append, Insert and Remove – even when you have set a size at declaration:

Dim arr(3) As Byte arr.Append(98) –> arr will contain 0, 0, 0, 0, 98

Dim encoded(-1) As Byte encoded(0) = 123 // I encounter a OutOfBoundException
So now you know why: because it is an empty array.
This will work:

Dim encoded(-1) As Byte encoded.Append(123)
And this too:

Dim encoded(-1) As Byte ReDim encoded(0) // instead of 0 there could also be a (possibly complex) calculation encoded(0) = 123

Thanks for the answers guys! Really helped me a lot.

What could be the reason that a Dim statement only takes constants, but ReDim can handle integers or numerical operations?

A Dim statement is evaluated at compile time, a ReDim statement at runtime.