Static (Member)
Declaration specifier for a static member procedure.
Syntax
`
Type typename
Declare Static membertype membername ...
End Type
`
Parameters
`typename`
name of a user defined data type
`membertype`
`Sub` or `Function`.
`membername ...`
Name of the member to declare or define with parameter list or return value following.
Description
When declaring a member procedure, `Static` specifies that the member procedure not have an implicit instance argument passed to it, and thus can only access values passed to it. Static member procedures may access Private or Protected members in their type, but to call procedures you must pass the Object as an additional first parameter. You may call a static member procedure anywhere in your code just like any other procedure in a type, i.e. variable of the type.static procedure name(arguments). The address of a static member function can also be assigned to a function pointer using the standard Procptr() or the @ Operator usage.
`Static` is allowed in front of member procedure bodies, not just in the declaration (the coherence with declaration is checked by compiler). When the procedure body is far from the declaration, it may be suitable to have here the information.
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`