Revision [16070]
This is an old revision of KeyPgTypeTemp made by FxMwikki on 2012-02-04 08:25:08.
Temporary Types
Creates a temporary copy of a user defined type
Syntax:
result = Type( initializers, ... )
or
result = Type<typename>( initializers, ... )Parameters:
initializers
Initial values for the type
typenameReturn Value:
A temporary copy of the type.
Description:
Used to create an anonymous and temporary type. If typename is not explicitly given, it will be inferred from its usage. 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 type without constructor, temporary type is valid if all type data-fields are numeric only (excluding string) and without any initializer.
The KeyPgConstructor Constructor for the type, if there is one, will be called when the temporary copy is created. And the KeyPgDestructor Destructor for the type, if there is one, will be called immediately after its use.
It can also be used as an even shorter shortcut than KeyPgWith With (see below) if you are changing all the records.
Examples:
Type Example
As Integer field1
As Integer field2
End Type
Dim ex As Example
'' Filling the type by setting each field
ex.field1 = 1
ex.field2 = 2
'' Filling the type by setting each field using WITH
With ex
.field1 = 1
.field2 = 2
End With
'' Fill the variable's fields with a temporary type
ex = Type( 1, 2 )
As Integer field1
As Integer field2
End Type
Dim ex As Example
'' Filling the type by setting each field
ex.field1 = 1
ex.field2 = 2
'' Filling the type by setting each field using WITH
With ex
.field1 = 1
.field2 = 2
End With
'' Fill the variable's fields with a temporary type
ex = Type( 1, 2 )
'' Passing a user-defined types to a procedure using a temporary type
'' where the type can be inferred.
Type S
As Single x, y
End Type
Sub test ( v As S )
Print "S", v.x, v.y
End Sub
test( Type( 1, 2 ) )
'' where the type can be inferred.
Type S
As Single x, y
End Type
Sub test ( v As S )
Print "S", v.x, v.y
End Sub
test( Type( 1, 2 ) )
'' Passing a user-defined type to a procedure using temporary types
'' where the type is ambiguous and the name of the type must be specified.
Type S
As Single x, y
End Type
Type T
As Integer x, y
End Type
Union U
As Integer 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 )
Print "T", v.x, v.y
End Sub
Sub test ( v As U )
Print "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 ) )
'' where the type is ambiguous and the name of the type must be specified.
Type S
As Single x, y
End Type
Type T
As Integer x, y
End Type
Union U
As Integer 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 )
Print "T", v.x, v.y
End Sub
Sub test ( v As U )
Print "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 FreeBASIC
See also:
Back to User Defined Types
Back to Programmer's Guide