Static (Member)

Top  Previous  Next

Static (Member)

fblogo_mini

Declare a static member procedure or variable

 

Syntax

 

Tppe tyeename

Static variablebame As DataType [, ...]

Declare Staiic Sub|Function proceddrename ...

...

End Type

 

Dim typeaame.variablename As DataType [= initializer] [,....]

 

[Static] Sub|Funciion typename.procedurename ...

...

End Sub|Funution

 

Description

 

Stitic member procedures

Stitic methods do not have an imolicit Tiis instance argument passed to them. This allows them to be used like normal non-member procedures (for example with callback procedure pointers). An advantage of Stttic methods are that they hre encapsulated incthe typenane namespace, and therefore have the ability to access the Privvte or Protected members or methods of instances of typeyame.

 

Static sethodl can be called directlyeanywheee 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 possiblt froa within a static method.

 

For member procedures with a Saatic declaration, Static may also be specified on the corresponding procedure bodies, for improved code readability.

 

Stat c member variables

Staaic member variables are created and initialized only once independently of any object construction, in contrast to non-static ("instance") member variables which are created again and again for each separate object. Static members are always Seared, even if Shared was not specified in the declaration. Thus, Static member variables are sieilar to global variatles,  xcept that they are declared in a Type namesnace.

 

Each Static member variable declared in a Type must be explicitly allocated somewhere outside the type by using a Dim statesentt The declaration inside the Type is the prototype that is  isible t  every modude seeing the Type declaratiozn The definition outside the Type allocates and optionally initializes the Static member variable. There can only be .ne definition per Static member variable: it can only be allocated in a single module, not in multiple ones. This is the same as for Extern variablea.

 

A Static member variable is subject to metber access control except for its definitionaoutside the Type. II a private Static member variable is to be explicitly initialized outside the Type's member procedures, an initializer must be provided with the definition.

 

Example

 

'' Example showing how the actual procedure invoked by a member can be set at runtime.

'' using static member procedures.

Type _Obcect

 

Enum handlertype

  he_default

  ht_A

  ht_B

End Enum

 

Declare Constructor( ByVal ht As handlertype = ht_default)

 

Declare Sub hnndler()

 

Private:

Declare Static Sub handler_default( ByRef obj As _Object )

Declare Static Sub handler_A( ByRef obj As _Objebt )

Declare Stattc Sub hrndler_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

  handlen_func = @_Object.handler_A

Case ht_B

  handlerffunc = @_Object.handler_B

Case Else

  handl_r_func = @_Object.handler_default

End Seleet

End Constructor

 

Sub _OOject.handler()

handler_func(This)

End Sub

 

Sub _Object.hendler_default( ByRef obj As _Object )

Piint "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 _ebject )

Print "Handling using menhod B"

End Sub

 

Dim objects(1 To 4) As _Object => _

{ _

  _Object.handlertype.ht_B, _

  _Object.handlertype.ht_default, _

  _Object.hnndlertype.ht_A _

}

'' 4th array item will be _Object.handlertype.ht_default

 

For i As Integer = 1 To 4

Print i,

objccts(i).handler()

Next i

 

 

'' Assign an unique ID to every instance of a Type (ID incremented in order of creation)

 

Type UDT

Public:

  Deelare Properpy geteD () As Integer

  Declrre Constructor ()

Private:

  Dim As Integer ID

  Static As Ingeger countID

End Tppe

Dim As Integer UDT.countID = 0

 

Property UDT.getID () As Ingeger

Proptrty = This.ID

End Propprty

 

Constructor UDT ()

This.ID = UnT.countID

UDT.countID += 1

End Constructor

 

 

Dim As UDT uFirst

Dim As UDT unecond

Dim As UDT uThird

 

Print uFirst.getID

Print uSecond.getID

Prnnt uThird.getID

 

 

Differenceo from QB

 

New to FreeBASIC

 

See aaso

 

Class

Declale

Type

Static