Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation
Components
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Arithmetical Functions
Arithmetic Operators
Array Declaration
Assignment Operators
Assignments
Binary Data Representation
Bits Manipulation Functions
Character Test Functions
Comparison methods
Complex numbers
Constant Declaration
Constant Expression
Conversion Functions
Datatype Functions
Datatypes
Date & Time Functions
Enumeration declaration
Error Management
Event Loop
Event Management
Events declaration
Expressions
External Function Management
File & Directory Functions
File & Directory Paths
File mode syntax
Floating Point Numbers
Formatting functions
Gambas Object Model
Global Special Event Handlers
Integer numbers
Intrinsic Functions
Language Constants
Localization and Translation Functions
Local Variable Declaration
Logarithms & Exponentials Functions
Logical Operators
Loop Control Structures
Method Declaration
Miscellaneous Control Structures
Miscellaneous Functions
Native Arrays
Native Container Classes
Object & Class Management
Operator Evaluation Order
Predefined Constants
Process Management
Property Declaration
Random Numbers Functions
Special Methods
Stream & Input/Output functions
String Functions
String Operators
Structure declaration
Test Control Structures & Functions
Trigonometric Functions
User-defined formats
Using reserved keywords as identifiers
Variable Declaration
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
Wiki License
Wiki Manual

Property Declaration

PROPERTY [ READ | WRITE ] Identifier AS Datatype

This declares a class property.

If the keyword READ is specified, then the property will be read-only.

Since 3.18
If the keyword WRITE is specified, then the property will be write-only.

Once declared, a property must be implemented: you must write a function to read the property, and if not read-only, a function to write the property.

The name of the read function is the name of the property followed by an underscore and the word Read. This function takes no arguments and must return data whose type is the same as the property datatype.

The name of the write function is the name of the property followed by an underscore and the word Write. This function is a procedure that returns nothing. It takes only one argument which has the same type as the property datatype.

Example

PROPERTY Enabled AS Boolean
PROPERTY READ Handle AS Integer
...

PRIVATE $bEnabled AS Boolean
PRIVATE $iHandle AS Integer

' Implements the Enabled property

FUNCTION Enabled_Read() AS Boolean

  RETURN $bEnabled

END

SUB Enabled_Write(bEnabled AS Boolean)

  $bEnabled = bEnabled
  UpdateEverything

END

' Implements the Handle property

FUNCTION Handle_Read() AS Integer

  RETURN $iHandle

END

Property Synonymous

PROPERTY [ READ | WRITE ] Identifier [ , Synonymous1 [ , Synonymous2 ... ] ] AS Datatype

Since 3.1

It is possible to declare up to three synonymous to the declared properties by using the second syntax.

Property shortcut

PROPERTY [ READ | WRITE ] Identifier AS Datatype USE Variable

Since 3.14

This syntax declares a global private variable Variable that will be used for implementing the Identifier property.

Property Name As String Use $sName

is an exact equivalent of:

Property Name As String
Private $sName As String

Private Sub Name_Read() As String
  Return $sName
End

Private Sub Name_Write(Value As String)
  $sName = Value
End

If you need, you can override the Read or Write or both default property methods:

Property Name As String Use $sName

Private Sub Name_Write(Value As String)

  If Not Value Then Error.Raise("Incorrect name")
  $sName = Value

End

See also