Dim Statement

Declares variables and allocates memory for them.

Syntax

[ @Shared ] { Dim | Var } declarator [ , declarator ]…

Parts

@Shared
Optional modifier specifies that each variable can have a small scope but keeps its value during an application's full run-time.
Dim
Mandatory unless you use Var.
Mandatory if you use the directive @Option Dim with Dim. See @Option Directive for more information.
Var
Mandatory unless you use Dim
Mandatory if you use the directive @Option Dim with Var.
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.

Default types and values

If you do not give a data type, then the compiler selects a data type. If you supply an initializer, then 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, then 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 Int = 0
Dim b! As Real = 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