Skip to content

Var, Dim or both?

API 2.0 added Var as an alias for Dim. Var is more recognized by coders from other languages and that’s great. Easily readable code is the goal, so I use both for example defining a variable:

Var Result As String

While defining an array will be:

Dim Results() As String

In the first example I’m defining a variable, while in the second I’m creating an array of strings. It’s the little things (to me) that make my code more readable without adding comments.

For a little bit of history, when I first started coding in Basic it wasn’t necessary to declare a variable and symbols were used to define the type. Dim was only needed to create arrays and if memory is correct (and it’s been a very long time) arrays were fixed sized so an example might have been:

S$ = "Hello" REM The $ symbol defines this as a string
S% = 1 REM The % symbol defines this as an Integer
DIM T$(15) REM This creates an array of 16 Strings

Variable names were limited to 2 characters + the defining symbol which also meant that S% was a different variable to S$ and of course this lead to chaos so Microsoft reused the DIM statement to define variables and that’s the way it stayed until more enlightened times.

Wayne Golding has been a Xojo developer since 2005 and is a Xojo MVP. He operates the IT Company Axis Direct Ltd which primarily develops applications using Xojo that integrate with Xero Wayne’s hobby is robotics where he uses Xojo to build applications for his Raspberry Pi, often implementing IoT for remote control.