Home > lang > vardecl 
 en fr de es it nl pl pt pt_BR mk sq ca hu cs tr ar fa id ko ja ru zh zh_TW eo
Previous  Next  Edit  Rename  Undo  Search  Administration  
Documentation  
Warning! This page is not translated.  See english version 
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.

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 instanciated object.

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

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.

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:

As the declaration order is respected, so 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 hole.

Array declaration

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

See Array Declaration for more information.

See also

Array Declaration, Local Variable Declaration, Method Declaration, Using reserved keywords as identifiers, Datatypes, Structure declaration