Dim Statement

Declares variables and allocates memory for them.

Syntax

[ modifier ] { Dim | Var } declarator [ , declarator ]…

Parts

modifier
One of these:
  • @ReadOnly – specifies that each variable has its value set at run-time and cannot change subsequently. See @ReadOnly for more information.
  • @Shared – specifies that each variable can have a small scope but keeps its value during an application's full run-time. See @Shared for more information.
Dim or Var
Mandatory keyword. Dim and Var are usually equivalent.
If you use the directive @Option Dim, Var becomes an error. And if you use @Option Var, Dim becomes an error. See @Option Directive for more information.
declarator
Mandatory one or more declarations with a comma between each.

Declarator

name [ ( [ dimensions ] ) ] [ As type | In unit ] [ = initializer ]
name
Mandatory name for a variable
dimensions
Optional one or more integer expressions with a comma between each
type
Optional data type
unit
Optional unit of measure
initializer
Optional expression having a data type compatible with type or unit.

Instructions

Setting an initial value

TODO

Declaring multiple variables

Arrays

See Arrays for more information.

Default types and values

If you do not give a data type, the compiler selects a data type. If you supply an initializer, the variable's data type comes from the data type of the initializer. See the table that follows for the rules.

If you specify The compiler sees it as
Dim a = 42
Dim a As Int64 = 42
Dim b = 3.14159
Dim b As Real64 = 3.14159
Dim c = 2.54cm
Dim c In centimeters = 2.54cm
Dim d = "Hello"
Dim d As String = "Hello"

If you do not supply a data type or initializer, the name of the variable selects the data type.

If you specify
Dim a%, b!, c$, #d
The compiler sees it as
Dim a% As Int32 = 0
Dim b! As Real64 = 0.0
Dim c$ As String = ""
'??? Dim #d As Object = #Null

Shared lifetime

All variables declared at module level have shared lifetime if @Shared is specified or not.

Examples

TODO

See also