Static (Member)
Declare a static member procedure
Syntax
`
Type typename
Declare Static Sub|Function procname ...
End Type
Static Sub|Function typename.procname ...
...
End Sub|Function
`
Description
`Static` methods do not have an implicit `This` instance argument passed to them. This allows them to be used like normal non-member procedures (for example with callback procedure pointers), the only difference being that they are encapsulated in the `typename` namespace and have access to `typename`'s `Private` or `Protected` members.
`Static` methods can be called directly anywhere in code, like normal non-member procedures, or on objects of type `typename`, similar to non-static methods, however either way there is no implicit or explicit `This` (or explicit `Base`) access possible from within a static method.
For member procedures with `Static` in their declaration, `Static` can also be specified on the corresponding procedure bodies, for improved code readability.
Example
`
'' Example showing how the actual procedure invoked by a member can be set at runtime.
'' using static member procedures.
Type _Object
Enum handlertype
ht_default
ht_A
ht_B
End Enum
Declare Constructor( ByVal ht As handlertype = ht_default)
Declare Sub handler()
Private:
Declare Static Sub handler_default( ByRef obj As _Object )
Declare Static Sub handler_A( ByRef obj As _Object )
Declare Static Sub handler_B( ByRef obj As _Object )
handler_func As Sub( ByRef obj As _Object )
End Type
Constructor _Object( ByVal ht As handlertype )
Select Case ht
Case ht_A
handler_func = @_Object.handler_A
Case ht_B
handler_func = @_Object.handler_B
Case Else
handler_func = @_Object.handler_default
End Select
End Constructor
Sub _Object.handler()
handler_func(This)
End Sub
Sub _Object.handler_default( ByRef obj As _Object )
Print "Handling using default method"
End Sub
Sub _Object.handler_A( ByRef obj As _Object )
Print "Handling using method A"
End Sub
Sub _Object.handler_B( ByRef obj As _Object )
Print "Handling using method B"
End Sub
Dim objects(1 To 4) As _Object => _
{ _
_Object.handlertype.ht_B, _
_Object.handlertype.ht_default, _
_Object.handlertype.ht_A _
}
'' 4th array item will be _Object.handlertype.ht_default
For i As Integer = 1 To 4
Print i,
objects(i).handler()
Next i
`
Differences from QB

- New to FreeBASIC

See also

- `Class`
- `Declare`
- `Type`
- `Static`