# Dim a ()  VS  Dim a (-1)  

**URL:** <https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015>\
**Category:** Getting Started\
**Created:** [December 16, 2014, 10:06pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015 "2014-12-16T22:06:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Horacio\_Vilches](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/ad7895/32.png) [@Horacio\_Vilches](https://forum.xojo.com/u/Horacio_Vilches)\
**Post date:** [December 16, 2014, 10:06pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/1 "2014-12-16T22:06:04Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [December 16, 2014, 10:08pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/2 "2014-12-16T22:08:42Z")

</div>

> [@152914:@Horacio Vilches](#):
>
> 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](http://documentation.xojo.com/index.php/Dim)

---

<div class="post-metadata">

**Author:** ![Derek\_DiBenedetto](https://forum.xojo.com/user_avatar/forum.xojo.com/derek_dibenedetto/32/17867_2.png) [@Derek\_DiBenedetto](https://forum.xojo.com/u/Derek_DiBenedetto)\
**Post date:** [December 16, 2014, 10:26pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/3 "2014-12-16T22:26:43Z")

</div>

(-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).

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [December 16, 2014, 10:28pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/4 "2014-12-16T22:28:23Z")

</div>

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:

```auto
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:

```auto
Dim aNames() As String
```

So there is no difference.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [December 17, 2014, 1:02am UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/5 "2014-12-17T01:02:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ramon\_SASTRE](https://forum.xojo.com/user_avatar/forum.xojo.com/ramon_sastre/32/326_2.png) [@Ramon\_SASTRE](https://forum.xojo.com/u/Ramon_SASTRE)\
**Post date:** [December 17, 2014, 8:30am UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/6 "2014-12-17T08:30:09Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [December 17, 2014, 2:25pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/7 "2014-12-17T14:25:07Z")

</div>

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

```auto
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…

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [December 17, 2014, 2:33pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/8 "2014-12-17T14:33:27Z")

</div>

> [@153083:@Brock Nash](#):
>
> The thing I think is stupid is when you want to dim a multidimensional array.

This is true for one-dimensional arrays too.

> [@153083:@Brock Nash](#):
>
> Why the compiler can’t figure this out is beyond me…

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

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [December 17, 2014, 2:39pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/9 "2014-12-17T14:39:34Z")

</div>

> [@153089:@Eli Ott](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [December 17, 2014, 2:46pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/10 "2014-12-17T14:46:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [December 17, 2014, 2:53pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/11 "2014-12-17T14:53:52Z")

</div>

> [@153089:@Eli Ott](#):
>
> Because at compile time rows (and columns for two-dimensional arrays) is not known.

> [@153098:@Eli Ott](#):
>
> 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:

```auto
dim myArray(x)
```

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

`dim myArray(-1)
redim myArray(x)`

---

<div class="post-metadata">

**Author:** ![Syed\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@Syed\_Hassan](https://forum.xojo.com/u/Syed_Hassan)\
**Post date:** [December 17, 2014, 3:03pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/12 "2014-12-17T15:03:27Z")

</div>

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](http://documentation.xojo.com/index.php/Dim) .

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [December 17, 2014, 3:05pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/13 "2014-12-17T15:05:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [December 17, 2014, 3:06pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/14 "2014-12-17T15:06:46Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [December 17, 2014, 3:12pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/15 "2014-12-17T15:12:41Z")

</div>

> [@153106:@Brock Nash](#):
>
> There’s really no good reason why the compiler couldn’t resolve this for the users other than laziness:

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.

---

<div class="post-metadata">

**Author:** ![Syed\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@Syed\_Hassan](https://forum.xojo.com/u/Syed_Hassan)\
**Post date:** [December 17, 2014, 3:27pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/16 "2014-12-17T15:27:03Z")

</div>

> [@153107:@Eli Ott](#):
>
> Example 3 compiles without problem.

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”.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [December 17, 2014, 3:34pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/17 "2014-12-17T15:34:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [December 17, 2014, 3:38pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/18 "2014-12-17T15:38:49Z")

</div>

[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`

---

<div class="post-metadata">

**Author:** ![Syed\_Hassan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@Syed\_Hassan](https://forum.xojo.com/u/Syed_Hassan)\
**Post date:** [December 17, 2014, 3:57pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/19 "2014-12-17T15:57:17Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [December 17, 2014, 4:01pm UTC](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015/20 "2014-12-17T16:01:31Z")

</div>

[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!!!

[Next page](https://forum.xojo.com/t/dim-a-vs-dim-a-1/21015.md?page=2)
