Variable Initializers
Variable initializers are supported for initializing Arrays, variables and UDTs.
Arrays, variables and UDTs may be given a value at the time of their declaration using Dim, with the syntax shown above. Please note the important differences between initializing different types. Scalar variables are initialized as they would in a normal assignment, using an equals sign. UDTs and arrays are assigned with an equal sign followed by a greater than symbol (=>). Array values are given in comma delimited values enclosed by curly brackets, and UDT values are given in comma delimited values enclosed by parenthesis.
These methods of initializing variables can be nested within one another for complex assignments. For instance, to initialize a multidimensional array:
In this declaration, the values for the left-most dimension are given as 5-index arrays. Nesting allows for arrays of any dimension to be initialized.
UDTs and arrays can be nested within each other as well. For instance, the following code declares and initializes an array of UDTs.
For module-level, static, or global variables, initialized values must be constant expressions. FreeBASIC will report a compile-time error if otherwise.
Syntax
Dim scalar_symbol [AS DataType] = expression
Dim array_symbol ([lbound TO] ubound) [AS DataType] => { expression [, ...] }
Dim udt_symbol AS DataType = ( expression [, ...] )
Dim array_symbol ([lbound TO] ubound) [AS DataType] => { expression [, ...] }
Dim udt_symbol AS DataType = ( expression [, ...] )
Description
Arrays, variables and UDTs may be given a value at the time of their declaration using Dim, with the syntax shown above. Please note the important differences between initializing different types. Scalar variables are initialized as they would in a normal assignment, using an equals sign. UDTs and arrays are assigned with an equal sign followed by a greater than symbol (=>). Array values are given in comma delimited values enclosed by curly brackets, and UDT values are given in comma delimited values enclosed by parenthesis.
These methods of initializing variables can be nested within one another for complex assignments. For instance, to initialize a multidimensional array:
Dim array(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}
In this declaration, the values for the left-most dimension are given as 5-index arrays. Nesting allows for arrays of any dimension to be initialized.
UDTs and arrays can be nested within each other as well. For instance, the following code declares and initializes an array of UDTs.
Type mytype
var1 As Double
var2 As Integer
var3 As ZString Ptr
End Type
Dim MyVar(2) As mytype => _
{ _
(1.0, 1, @"Hello"), _
(2.0, 2, @"GoodBye") _
}
var1 As Double
var2 As Integer
var3 As ZString Ptr
End Type
Dim MyVar(2) As mytype => _
{ _
(1.0, 1, @"Hello"), _
(2.0, 2, @"GoodBye") _
}
For module-level, static, or global variables, initialized values must be constant expressions. FreeBASIC will report a compile-time error if otherwise.
Differences from QB
- Variable Initializers are new to FreeBASIC
See also