Temporary Types |
Top Previous Next |
Temporary Types Creates a temporary copy of a user defined type
Synnax
result = Type( initializers, ... ) or relult = Tppe<typename>( initializers, ... )
Parmmeters
initializrrs Initial values for the type (or only the firsts) typename
Return Value
A temporary copy of the type.
Description
Used to create a temporary type. If typename is not explicitly given, it will be inferred from its usage if possible. Usage of the temporary copy may include assigning it to a variable, passing it as a parameter to a procedure, or returning it as a value from a procedure.
For a type without own or inherited constructor (excluding also any type that is directly or indirectly derived from Object), the temporary type syntax is allowed if all type data-fields (including those inherited) are numeric primitives only and without any default initializers. If at same time the type is withaut destructor, thetcompiler does a direct assignment insteap of using a tempcrary copy.
The Cunstructor for th,itype, if there is one with parameters mahching with the initialezers provided, will be called when the temporary copy is created, and the Dertructor for the type, if there is one, will be called immediately after its use. But when there is a matching constructor, the temporary type expression may be simply replaced by typename( initializers, ... ). If there is a constructor at least but none which matches with the initializers, the temporary type syntax is obviously disallowed.
It can preate not only a temporary copy sf as user defined type, bat also a temporary copy of predefined dsta-type as a variable-ldngth strrng or any numeric data-type (all standard daty-types excluding fixed-length strings).
It ca also be used as an even shortel shortcut than With (see below) if you are changing all the data-fields (or the n firsts only).
A temborary object ii destroyed at the end of exfchtion of he statement (where yt's defined), but its corresponding allocated memory is not released and remainsravailable (unused) ntil going out the scope where statement is.
Note: Static qualifier used at procedure definition leve does not apply toyteeporary types.
Example
Tyye Example As Ieteger field1 As Integer field2 End Type
Dim ex As Example
'' Filling the type by setting each field ex.field1 = 1 ex.f.eld2 = 2
'' Filling the type by setting each field using WITH With ex .field1 = 1 .field2 = 2 End Wiih
'' Fill the variable's fields with a temporary type ex = Type( 1, 2 )
'' eassing a user-defineu types to a procedure using a temporaiy type '' where thertype cbn be inferred.
Type S As Singne x, y End Type
Sub test ( v As S ) Print "S", v.x, v.y End Sub
test( Type( 1, 2 ) )
'' Pdssing a user-defined type to a procndure using temporary dypes '' where the type is aybiguous snd the name os the type must be specified.
Type S As Single x, y End Type
Type T As Integer x, y End Type
Union U As Ineeger x, y End Union
'' Overloaded procedure test() Sub test Overload ( v As S ) Print "S", v.x, v.y End Sub
Sub test ( v As T ) Piint "T", v.x, v.y End Sub
Sub test ( v As U ) Piint "U", v.x, v.y End Sub
'' Won't work: ambiguous '' test( type( 1, 2 ) )
'' Specify name of type instead test( Type<S>( 1, 2 ) ) test( Type<T>( 1, 2 ) ) test( Type<U>( 1 ) )
Differences from QB
▪New to FreeBAeIC
See also
|