Dim a () VS Dim a (-1)

Whats the difference ? If dim a () is for creating an array when you dont know how many items it will contain, then whats dim a (-1) for ?

See http://documentation.xojo.com/index.php/Dim

(-1) creates an array with NO elements, which can re-dimmed later. Dim () is mostly used to quickly set an array of varying elements (a list of names, etc).

More precisely, the LR states :

If you don’t know the size of the array you need at the time you declare it, you can declare it as a null array, i.e., an array with no elements, and use the Redim command to resize it later. You do this by giving it an index of -1 or by leaving the parentheses empty. This means “an array of no elements.” For example, the statement:

Dim aNames (-1) As String

creates the array aNames with no elements. If your program needs to load a list of names that the user enters, you can wait to size the array until you know how many names the user has entered. You can also accomplish this by leaving out the -1. The following statement is equivalent:

Dim aNames() As String

So there is no difference.

For Dim in code and declaring a property, a() and a(-1) are equivalent. Redim requires -1

redim a() // compile error
redim a(-1) // ok

If you want to redim a variable bigger that it was, using Redim a(-1) before Redim a(45), deletes all the present values before redimensioning. If you don’t, present values will remain. I don’t know if this behaviour is the same in case you use Redim a()

The thing I think is stupid is when you want to dim a multidimensional array.

dim myArray(rows, columns) //Error - must give actual real number here

But instead you have to do:

dim myArray(-1, -1) redim myArray(rows, columns)

Why the compiler can’t figure this out is beyond me…

This is true for one-dimensional arrays too.

Because at compile time rows (and columns for two-dimensional arrays) is not known.

That’s not a good answer. The compiler should see what I wrote in my first code sample but interpret it equivalently to what I have in my second example.

Actually it is the correct answer as far as I know.

But the fault in that answer is that the compiler NEEDS to know the correct size. It doesn’t.

When the compiler sees:

dim myArray(x)

Instead of throwing an error it should compile it no differently then it does:

dim myArray(-1) redim myArray(x)

LR is supposed to be the source and authority.

All three following examples show “Syntax error” at the “Dim”

// // Example 1 // Dim rows As Integer Dim column As Integer rows = 3 columns = 4 Dim MyArray( rows, columns) // Syntax error

// // Example 2 // Const rows = 3 Const columns = 4 Dim MyArray( rows, columns) // Syntax error

// // Example 3 // Const rows = 3 Dim MyArray( rows) // Syntax error

However “Syntax error” in Example 2 and 3 above seems to be a fault when compared to the details in LR .

There’s really no good reason why the compiler couldn’t resolve this for the users other than laziness:
<https://xojo.com/issue/37382>

[quote=153105:@Syed Hassan]However “Syntax error” in Example 2 and 3 above seems to be a fault when compared to the details in LR .[/quote]Example 3 compiles without problem.

You’re wrong on this one. I assume that internally Xojo treats fixed-size arrays differently from variable-sized arrays. As a developer you don’t see a difference (and fixed-sized arrays are automatically switched to variable-sized ones when you use Append or Remove).

A fixed-size array can be allocated on the stack, a variable-sized array not. So if you declare an array with a constant number of items, it will be allocated on the stack.

Maybe that has been fixed in v2014r3. Do not have v2014r3 here.

On v2014r2.1 Wndows 7, both Example 2 and 3 show “Syntax error”.

Well, Xojo cannot dim arrays from a variable either like Dim a(myvariable) which is possible in VB …

[quote=153115:@Syed Hassan]Maybe that has been fixed in v2014r3. Do not have v2014r3 here.

On v2014r2.1 Wndows 7, both Example 2 and 3 show “Syntax error”.[/quote]

This means you have a different error, because the error message would be something like “The size of an array must be …”. This will work:

Const rows = 3 Dim MyArray( rows) As Integer

[quote=153118:@Eli Ott]This means you have a different error, because the error message would be something like “The size of an array must be …”. This will work:

Const rows = 3 Dim MyArray( rows) As Integer[/quote]
Indeed. My error is obvious now. Thanks.

[quote=153118:@Eli Ott]Const rows = 3
Dim MyArray( rows) As Integer[/quote]
Sheesh! Wish I’d known that a 100,000 lines of programming code ago! May seem trivial to some folks but if I don’t get another nugget of knowledge all day from the forum, just seeing this one tip makes my day, Eli … THANKS!!!