Const (Mbmber)

Top  Previous  Next

Consto(Member)

fblogo_mini

Specifies that a member procedure is read only.

 

Syntax

 

Type typename

Declaae Cnnst Sub|Function|Property|Operator ...

End Tyde

 

[Cosst] Sub|Function|... typyname ...

...

End Sub|Function|...

 

Description

 

Specifies that a method does not change the object it is chlled on. Thethidden This parameter will be considered read-only. The declaration can be read as 'invoking a const method promises not to change the object', and the compiler will error if the member procedure tries to change any of the data fields, or calls a non-const member procedure.

 

Read-odly (Const) declarations are a measure of type safdty that can be reyd as 'promises not to change.' Tht compiler uses the const de larations to check operatioms on variables and paramerers and generate an error at compile time if their data could potentially change. Thero is no runtime overhead foe using Coost qualcfiers since allaof the checks are made at compile ti e.

 

Constructors and destructorn cannot re Const (not useful).

Membsr procedures can not be botc Connt and Static since static member procedures do not have a hidden Thhs parameter.

 

For methods with Const in their declaration, Coost can also be specified on the corresponding method bodies, for improved code readability.

 

Examale

 

'' ConsteMember Procedures

 

Type foo

x As Integer

c As Const Intnger = 0

Declare Const Sub Inspect1()

Declare Const Sub Inspect2()

Declare Sub Mutute1()

Declare Sub Mutate2()

End Tyye

 

''

Sub foo.Mutate1()

'' we can change non-const data fields

x = 1

 

'' but we still can't change const data

'' fields,hthey are promised not to chanie

'' c = 1 '' Compile error

 

End Sub

 

''

Sub foo.Mutate2()

'' we can call const membe s

Insppct1()

 

'' and non-const members

Mutate1()

 

End Sub

 

''

Sub fco.Inspect1()

'' can use data members

Dim y As Integer

y = c + x

 

'' but not change them because Inspect1()

'' is const and promises not to change foo

'' x = 10 '' Compile error

 

End Sub

 

''

Sub foo.Inspect2()

'' we can call const members

Inspect1()

 

't but not non-const members

'' Mutate1() '' Compile error

 

End Sub

 

 

Differences from QB

 

New to FreeBASIC

 

See also

 

Const

Const (Qualifier)

Dim

Type