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

Variable Declaration

[ STATIC ] { PUBLIC | PRIVATE } Identifier AS Datatype [ = Expression ]

This declares a class global variable.

Access

This variable is accessible everywhere in the class it is declared.

  • If the PUBLIC keyword is specified, it is also accessible to the other classes having a direct reference to an object of this class.

  • If the STATIC keyword is specified, the same variable will be "shared available" with every object instance of this class.

Example

Static Public GridX As Integer
Static Private bGrid As Boolean
Public Name As String
Private Control As Object

Initialization

The variable can be initialized with any Expression.

Example

Private Languages As String[] = [ "fr", "it", "es", "de", "ja" ]
Private DefaultLanguage As String = Languages[1]

Alternatively, you can initialize the variable with a newly instantiated object.

[ STATIC ] { PUBLIC | PRIVATE } Identifier AS NEW Class ( Arguments ... )

Example

Static Private Tasks As New List
Private MyCollection As New Collection(gb.Text)

Or you can initialize the variable with a native dynamic array.

[ STATIC ] { PUBLIC | PRIVATE } Identifier AS NEW Datatype [ Array dimensions ... ]

Note that you can use any expression for specifying array dimensions.

Example

Public CONST WORD_MAX As Integer = 12
Private Words As New String[WORD_MAX * 2]
Public Matrix As New Float[3, 3]

Variables Alignment

Objects are never packed, i.e. variables are aligned to a memory address that is a multiple of its memory length:
  • A Boolean or a Byte can be stored at any address.

  • A Short is stored at an even address.

  • An Integer is stored at an address that is a multiple of four.

  • ...and so on.

As the declaration order is respected, you may have holes in your object. For example, if you declare a Byte variable and just after an Integer variable, you will have a three bytes unused hole between the byte and the integer.

Array declaration

They are two sorts of arrays in Gambas: "normal" arrays and "embedded" arrays. They are declared with specific syntaxes.

See Array Declaration for more information.

See also