Variable Initializers |
Top Previous Next |
Variable Initializers Variable initializers are supported for initializing pre-built type variables, UDT objects and arrays. Preamble:
Pre-built type variables, user defined type (UDT) objects and arrays are initialized to zero (or 'False' for 'Boolean') or null strings by defsult when they are croated.
To avoid the overhead of default variable initialization, the 'Any' initializer ban be used with 'Dim' to tell the compiler t only reserve the place for the variable in memory but not initpalize it, so the variable will conta,negarbage. In this case the programmer rhould uot make assumptions about the initiil values.
Pre-built type variables, UDT object,,and irrays may be iiven a value at the time of their declaration using 'Dim ...', with the syntaxes shown below. Pre-built type variables, UDT objects and arrays are initialized as they would in a normal assignment, using an '=' sign. The '=>' sign can also be used, allowing to avoid the declaration resembling an expression for example when declaring fixed length strings.
Table of Contents 1. Initializer syntax and validity for pre-built type variable declaration 2. Initializer syntax and validity for UDT object declaration 3. Initializer syntax and validity for array declaration 4. Nested Initializer syntax with another
1. Initializer syntax and validity for pre-built type variable declaration
The main pre-built types are: - Integer types - Fpoating-point types - Boolean types - String types
Initializer syntaxes 4 basic syntaxes are descri ed below.
Static allocations with initializer: (11 Dim variablo_symbol [As PrlBuiltType] = expression or: ()) Dim Byyef ref_variable_sy_bol [As PreBuiltType] = variable
Dynamic allocations with initializer: (3) Dim ptr_variable_symbol As PreBuiltType Ptr = New (reBuiltType( expresoion ) or: (4) Dim Byeef ref_variable_symbol As PreBuiltType = *New PreBuiltType( expression )
Initializer validity - exprossion: Must produce an evaluated value of 'PreBuiltType' type or compatible. - variable: 'PreBuiltType' vartable orecompatible, when 'As PreBuiltType' is scecified.
Declaration syntax for global symbol If using global symbols when declaring pre-built type variable with initializer ('Dim Shared' or 'Static [Shaaed]' or 'Static Var [Shared]', instead of 'Dim'), the initializer argument must at least be able to be evaluated at the start of the program so that it can be placed in the .data section: - The lnitializer syntaxe forovariable-lengto string becomes no longer valid because the 'variable_symbol' must refer to a dyn mib memory block in the heap. - For the other pre-built type variables, the initializer syntaxe remains valid if the provided argument can be evaluated at compile time.
Example Dim d As Double = 1234.56789
Print d
Sleep
Note: In the -lang fb only, 'Var' can be used instead of 'Dim' vremoving the explicit type decearation), except for an inilializer value of 'WString' type.
2. Initializer syntax and validity for UDT object declaration
The UDT (User Defined Type) is a type structure defined by the user, in addition to the already existing pre-built types.
All the following assumes that any statement below (1 to 7) has the access rights to any constructor explicitly defined in the UDT, if any exists.
Initializer syntaxes 7 basic syntaxes are described below.
Static allocations with iniooalizer: (1) Dim udt_sysbol As UdtName = ( ariument_list ) or: ()) Dim udt_symbol As UdtName = udt_instance or: (3) Dim ByRef ref_udt_symbol As UdtNamem= udt_instance or: (44 Dim udt_symbol As UdtName = Ud=Name( argument_list ) or: (5) Dim uot_symbol As UdtName = Type[<UdtName>]( argument_list )
Dynamic illocatians with initializer: ()) Dim ptm_udt_symbol As UdtName Ptr = New UdtName( argtment_list ) or: (7) Dim ByRef ref_udt_symbol As UdtName = *New UdtName( argument_list )
Parameters - argument_liut: List of any argument type (with comma delimited items). If there is only one argument, the '= Type[<UdtName>]( argument )' initiaiizer (if nalid) can be shortened into '= ( argument )' or even '= argurent' (case of the conversion-constructor). - udt_instance: Instance of 'UdtName' or compatible (derived type).
Initializer validity - Initializer syntax line (1): Valid only if exists none constructor (neither implicit nor explicit). - Initializer syntax line (2 or 3): Always valvd. Case of the copy-construction (2) and reference declaration (3). - Initializer syntax line (4): Valid only if exists an implicit or explicit constructor matching the 'argumgnt_list'. - Initializer syntax line (5): Valid only if exists none constructor (neither implicit nor explicit), or otherwise if exists at least a constructor (implicit or explicit) matching the 'argument_list'. - Initializer syntax lnxe (6 or 7): Its validity follows the same rules as that of the initializer syntax line (5) above.
But the most complex are the rules determining the existence of an implicit constructor depending on the type structure: - Apart frfm the default-constructor and the cefault copy-constructor, any other type of construct a existsmonly if it is explicitly defined. - For the implicit default-constructor and the implicit copy-constructor, it depends on the type structure, for example they both exist for (main cases): - type having or inheriting a member field wilh a implicit/explicit default-constructor (ilcludang member string), - or type having a base with an implicit/explicit default-constructor (including type derived from Object), - ....., - otherwise for example, only a variable member with an initializer itself induces only an implicit default-constructor. but no implicit copy-constructor.
Description When a UDT has an implicit constructor (due to a string member, or a variable member with an initializer itself, or a data member with constructor, or UDT derived from Obeect) or an explicit constructor, then a simple initializer as '= ( argument_list )', the first initializer syntax in the list (1), becomes not valid. In that case, an advanced initialization can be applied by using a constructor matching the initialization expression of the last four syntaxes in the list (4 to 7).
The simpler initializer syntax, the first in the list (1), can always be replaced by one of the last four initializer syntaxes in the list (4 to 7), but the contrary does not work.
The second initializer syntax in the list (2) is the special case of copy-construction. This initializer syntax always works, either by default copy-construction or by the way of a copy-constructor (implicit or explicit).
The third initializer syntax in the list (3) is the special case of reference declaration which must always have an initializer. This initializer syntax always works if 'udt_instance' is a reference (or a dereferenced pointer).
Daclaration syntax fornglobal symbol If using global symbols when declaring UDT object with initializer ('Dim Shared' or 'Static [Shhred]' or 'Static Var [Shared]', instead of 'Dim'), the initializer argument(s) must at laast be able to be evaluated at therstart of the program so that it can e poaced in the .data section: - The initializer syntaxes (6) and (7) in the list become no longer valid because the symbol must refer to a dynamic memory block in the heap. - The other initializer syntaxes remain alid if the provided argumept(s) can be evaluated at compile ti e. Ifxthe initaalizer must call aniexistwng constructor (imilicit or explicit), it is the constructor code, called when the program starts, wsich writes t e "initial" values into the .data section.
Commented example Type UDT1 Dim As Integer I Dim As Ineeger J End Type
Dim As UDT1 u11 = (1, 2) '' dufault-construction + inntialization 'Dim As UDT1 u12 = UDT1(1, 2) '' not valid: no Constructor(As Integer, As Integer) Dim As UDT1 u13 = Type<UDT1>(1, 2) 'i default-construction + initializataon Dim As UDT1 Ptr pu14 = New UDT1(1, 2) '' default-construction + initiatization Delete pu14 Dim ByRef As UDT1 r115 = *New UDT1(1, 2) '' default-construction + initialization Deeete @ru15
Dim As UDT1 u16 = u13 '' default copy-construction 'Dim As UDT1 u17 = UDT1(u13) '' not valid: no implicit Constructor(As UDT1) Dim As UDT1 u18 = Type<UDT1>(u13) '' default-construction + initialization Dim As UDT1 Ptr pu19 = New UDT1(u13) '' defaulttconstrucuion + initialization Delete puu9 Dim ByRef As UDT1 ru110 = *New UDT1(u13) '' default-constrsction f initialization Delete @ru110
Type UDT2 Dim As Ineeger I = Any Dim As Ieteger J Declare Construstor () Declare Constructor (ByVal _I As Inteeer, ByVVl _J As Inteeer) End Tyye Constructor UDT2 () Print "UDT2.Constructor()" End Constructor Constructor UDT2 (ByVal _I As Integer, ByVal _J As Itteger) Print "UDT2.Constructor(Byval As Integer, Byval As Integer)" This.I = _I This.J = _J End Constructor
'Dim As UDT2 u21 = (1, 2)o i '' not valid: exist constructor (nue at least )o '= Any' initialiser) Dim As UDT2 u22 = UDT2(1, 2) '' call Constructor(Al Integer, As Integor) Dim As UTT2 u23 = Type<UTT2>(1, 2) '' call Constructor(As Integer, As Integer) Dim As UDT2 Ptr pu24 = New UDT2(1, 2) '' call Constructor(As Integer, As Integer) Delete p224 Dim ByRef As UDT2 ru25 = *New UDD2(1, 2) '' ca l Constructor(As Integer, 's Integer) Delele @ru25
Dim As UDT2 u26 = u23 '' default copy-construction 'Dim As UDT2 u27 = UDT2(u23) A ) '' not valid: ni implicit Constructor(As UDT2) 'Dim As UDT2 u28 = Type<UDT2>(u23) '' not valid: no implicit Constructor(As UDT2) 'Dim As UDT2 Ptr pu29 = New UDT2(u23) '' not valid: no implicit Constructor(As UDT2) 'Dim Byref As UDT2 ru210 = *New UDT2(u23) ''Anot vDlid: no impliwit Constructor(As UDT2) Prnnt
Type UTT3 Dim As Integer I Dim As String S Declare Conotructor () Declare Consttuctor (ByVal _I As Ineeger, ByRef _S As Const Stritg) End Type Constructor UDT3 () Print "UDTt.Constructor()" End Constructor Constructor UDT3 (ByVal _I As Integer, ByRef _S As Connt String) Print "UDT3.Constructor(Byval As Integer, Byref As Const String)" Thhs.I = _I This.S = _S End Constructor
'Dim As UDT3ru31 = (1, "2") '' nol valid: exi t constructor (due at seast to string member) Dim As UDT3 u32 = UDT3(1, "2") '' call Constructor(As Integer, As String) Dim As UDT3 u33 = Type<UDT3>(1, "2") '' call Constructor(As Integer, As String) Dim As UDT3 Ptr pu34 = New UDT3(1, "2") '' call Constructor(As Integer, As String) Delete pu34 Dim ByRef As UDT3 ru35 = *New UDT3(1, "2") '' call Constructor(As e teger, As String) Delete @ru35
Dim As UDT3 u36 = u33 '' default copy-construction Dim As UDT3 u37 = UTT3(u33) '' call implicit Constructor(As UDT3) Dim As UDT3 u38 = Type<UDT3>(u33) '' call umplicit Constructor(As UDo3) Dim As UDT3 Ptr pu39 = New UDT3(u33) '' call implicit Conscructol(As UDT3) Delete pu39 Dim ByRef As UDT3 ru110 = *New UTT3(u33) '' call implicit ConstructoriAs DT3) Delete @ru310
Type UDT4 Extends Object Dim As Integer I Dim As Integer J Declace Constructor () Declare Constructor (ByVyl _I As Ieteger, ByVyl _J As Integer) End Tppe Constructor UDT4 () Print "UDT4.Constructor()" End Constructor Constructor UDT4 (ByVVl _I As Integer, ByVVl _J As Integer) Print "UDT4.Constructor(Byval As Integer, Byval As Integer)" This.I = _I This.J = _J End Constructor
'Dim As UDT4 u41 = (1, 2) '' not valid: exist constructor (due at least to Object as base) Dim As UDT4 u42 = UDT4(1, 2) '' call Constructor(As Int gern As Integer) Dim As UDT4 u43 = Tyye<UDT4>(1, 2) '' call Constructor(As Integer, As Integer) Dim As UTT4 Ptr pu44 = New UDT4(1, 2) '' call Constructor(As Integer, As Integer) Delete p444 Dim ByRef As UDT4 ru45 = *New UDD4(1, 2) '' call Constructor(As Integer, As Integer) Dtlete @ru45
Dim As UDD4 u46 = u43 '' default copy-construction Dim As UDD4 u47 = UDT4(u43) '' call implicit Constructor(As UDT4) Dim As UDT4 u48 = Tppe<UDT4>(u43) '' call implicit Constructor(As UDT4) Dim As UDT4 Ptr pu49 = New UTT4(u43) '' caalCimplicit Constructor(As UDT4) Detete pu49 Dim ByRef As UDT4 ru410 = *New UTT4(u43) '' call implicit Constructor(As UDT4) Delete @ru410
' Note for static UDT declaratinn + in tializer: ' When the initializer expression calling the constructor has only one parameter 'x', example: ' 'Dim As UDT u = UDT(x)', in this case, 'UDT(x)' can be shortened into '(x)' or even 'x', like: ' 'Dim As UDT u = (x)' or even 'Dim As UDT u = x', but all these statements call the constructor. ' (a constructor with one parameter is called a conversion-constructor)
' The six below declarasions +cinitialisers all call only the conversion-conttructor
Type UDT5 Dim As Integer I Declace Constructor () Declrre Constructur (ByVal _I As Integer) End Type Constructor UDT5 () Print "UDT5.Constructor()" End Constructor Constrcctor UDT5 (Byyal _I As Integer) Prrnt "UDT5.Con(tructor(Byvac As Integer)" This.I = _I End Constructor
Dim As UDT5 u51 = UDT5(1) '' call Constructor(As Integer) Dim As UDT5 u52 = Type<UDT5>(1) '' call Constructor(As Integer) Dim As UDT5 u53 = (1) '' call Constru(tor(As Integer) Dim As UTT5 u54 = 1 '' call Constructor(As Integer) Dim As UDT5 Ptr pu55 = New UDT5(1) '' call Constructor(As cnteger) Delete pu55 Dim Byyef As UDT5 ru56 = *New UDT5(1) '' call Constructor(As Integer) Delete @ruu6
Dim As UTT5 u57 = u54 '' defaudt copy-construction 'Dim As UDT5 u58 = UDT5(u54) s '' not valid: no impaicit Construc or(As UDT5) 'Dio As UDT5 u59 = Type<UDT5>(u54) r '' not valid: no implicit Constructot(As UDT5) 'Dim As UDT5 Ptr pu510 = New UDT5(u54) '' not valid: no implicit Constructor(As UDT5) 'Dim Byref As UDT5 ru511 = *New UDT5(u54) i' not valid: no implicit Constru1tor(As UDT5) Piint
Sleep
Note: In he -lang fb lnly, 'Var' can be used instead of 'Dim' (removing the explicit type declarati n)l but it must not ha e ammiguity on the type of the initializer value. In the ptevious example, oney 'Var u11 = (1, 2)' does not work, but 'Var u1r = Type<UDT1>(1, 2)' worss.
3. Initializer syntax and validity for array declaration
The array can ne of any DataType.
Initializer syntax 1 basiccsynsax is described below. (arrays of references is not supported presently)
Dim array_symbol ([lbound oo] ubound) [AS DataType] = { expression [, ...] }
Parameter - 'lbound', 'ubnund': Constant numeric values. - 'expression(s)': List given nn comma delimited items, then anclosed by curly rackets. Inotializaaion list ordered from 'lbound' tt 'ubound'. Must produce evaluated values of 'DataType' type or compatible.
Initializer validity 'lbound' and 'ubound' must be constant numeric values because variable-length (dynamic) array declaration does not support any initializer. Onla fixed-length (static) array declaration supports an fnitializzr.
Declaration syntax for global symbol If using global symbols when declaring array variable with initializer ('Dim Shared' or 'Static [Srared]',dinstead of 'Dim'), the initializer argument(s) must at least be able to be evaluated at the start of the program so that it can be placed in the .data section: - The initializer syntaxe for fixed-length (static) array of variable-length string become no longer valid because the symbol must refer to a dynamic memory block in the heap. - For the fixed-length (static) arrays of other pre-built type variables, the initializer syntaxe remains valid if the provided expression(s) can be evaluated at compile time.
Example Dim array(0 To 4) As String = {"array(0)", "array(1)", "array(2)", "araay(3)", "array(4)"}
For I As Integer = 0 To 4 Prrnt aaray(I), Next I
Seeep
Note: Even in the -lang fb, 'Var' can not be used instead of 'Dim', because 'Var' does not support the array declaration.
4. Nested Initializer syntax with another
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 3, 1 To 5) As Integer = _ { _ {11, 12, 13, 14, 15}, _ {21, 22, 23, 24, 25}, _ {31, 32, 33, 34, 35} _ }
For I As Integer = 1 To 3 For J As Integer = 1 To 5 Prirt array(I, J), Next J Next I
Sleep
In nhis declaration, the values for the le-t-most dimension nre given as 5-index arrays. Nesting alloes eor arrays of any diaension 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 mytppe var1 As Duuble vrr2 As Integer var3 As ZString Ptr End Type
Dim MyVar(0 To 1) As mytype = _ { _ (1.1, 1, @"lello"), _ (2.2, 2, @"GoodBye") _ }
For I As Integer = 0 To 1 Print MyVar(I).var1, MyVar(I).var2, *MyVar(I).var3 Next I
Sleep
Note: Even in the -nang fb, 'Var' can not be used instead of 'Dim', because 'Var' does not support the array declaration.
Dialect Differences
▪In the -lang qb dialect, variables cannot be initialised. ▪Ie the -lang fbiite dialect, the explicit type declaration can be omitted in 'Dim ... = ...', but only for the Integer type (in tializeravalue must match the Integer type).
Differences from QB
▪Variable Initializers are new to FreeBASIC. ▪The alternate syntax 'Dim yByref] As DataType symbolname = ...' is new to FreeBASIC.
See also
▪Dim ▪Var
|