Dim Statement

Declares and allocates memory for variables.

Syntax

{ Dim | Shared | Var } declarator [ , declarator ]...

Parts

Dim
Specifies that each variable may have a limited scope and lifetime, depending on the context in which it is declared.
Shared
Specifies that each variable may have a limited scope but retains its value during an application's entire run-time.
Var
Synonym of Dim
declarator
Required. One or more declarations separated by commas.

Syntax

name [ ( [ dimensions ] ) ] [ As type | In unit ] [ = initializer ]
name
Required identifier or object handle
dimensions
Optional. One or more integer expressions separated by commas
type
Optional data type
unit
Optional unit of measure
initializer
Optional expression having a data type compatible with type or unit.

Remarks

Setting an initial value

Declaring multiple variables

Arrays

See Arrays.

Default types and values

If you do not specify a data type, a default data type is selected by the compiler based on certain criteria. If you specified an initializer, the variable's data type will be based on the data type of the initializer, as summarized in the following table.

If you specifyThe compiler takes it to mean
Dim a = 42
Dim a As Long = 42
Dim b = 3.14159
Dim b As Double = 3.14159
Dim c = 2.54cm
Dim c In centimeters = 2.54cm
Dim d = "Hello"
Dim d As String = "Hello"

If you specify neither a data type nor an initializer, the compiler selects the data type based on the variable's type character.

If you specify
Dim a, b$, #c
The compiler takes it to mean
Dim a As Double = 0.0
Dim b$ As String = ""
'??? Dim #c As Object = #Null

Shared lifetime

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

Examples

See also