Destructor
Called automatically when a class or user defined type goes out of scope or is destroyed
Syntax
`
Type typename
field declarations
Declare Destructor ( )
End Type
Destructor typename ( )
statements
End Destructor
`
Parameters
`typename`
name of the `Type` of `Class`
Description
The destructor method is called when a user defined `Type` or `Class` variable goes out of scope or is destroyed explicitly with the `Delete` operator.
The `Destructor` method is passed a hidden `This` parameter having the same type as `typename`.
The destructor in a type is called before the destructors on any of its fields. Therefore, all fields are accessible with the hidden `This` parameter in the destructor body.
Only one destructor may be declared and defined per type.
Since the `End` statement does not close any scope, object destructors will not automatically be called if the `End` statement is used to terminate the program.
Example
`
Type T
value As ZString * 32
Declare Constructor ( init_value As String )
Declare Destructor ()
End Type
Constructor T ( init_value As String )
value = init_value
Print "Creating: "; value
End Constructor
Destructor T ()
Print "Destroying: "; value
End Destructor
Sub MySub
Dim x As T = ("A.x")
End Sub
Dim x As T = ("main.x")
Scope
Dim x As T = ("main.scope.x")
End Scope
MySub
`
Output:

    Creating: main.x
    Creating: main.scope.x
    Destroying: main.scope.x
    Creating: A.x
    Destroying: A.x
    Destroying: main.x

Dialect Differences

- Object-related features are supported only in the -lang fb dialect.

Differences from QB

- New to FreeBASIC

See also

- `Class`
- `Constructor`
- `Delete`
- `Destructor (Module)`
- `Type`