Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation from source code
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

Local Variable Declaration

DIM Identifier AS Datatype [ = Expression ]

Declare a local variable in a procedure or function.

This variable is only accessible to the procedure or function where it is declared.

Examples

DIM iVal AS Integer
DIM sName AS String
DIM hObject AS Object
DIM hCollection AS Collection

Initialization

The variable can be initialized with any expression.

Examples

DIM bCancel AS Boolean = TRUE
DIM Languages AS String[] = [ "fr", "it", "es", "de", "ja" ]
DIM DefaultLanguage AS String = Languages[1]

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

DIM Identifier AS NEW Class ( Arguments ... )

Examples

DIM aTask AS NEW String[]
DIM aCollection AS NEW Collection(gb.Text)

Or you can initialize the variable with a native dynamic array. See Array Declaration for more information.

Multiple Declarations

You can declare several variables on the same line:
  • Each declaration must be separated by a comma.

  • You can specify the identifier only. It will have the same declaration as the first next identifier having a complete declaration.

Examples

DIM Text AS String, Matrix AS NEW Float[3, 3]
DIM X, Y, W, H AS Integer

Local static variable declaration

Since 3.17

STATIC Identifier AS Datatype [ = Expression ]

Declare a local static variable in a procedure or function.

A local static variable keep its value when the function terminates. It is really like a global static variable, but that is only accessible to the procedure or function where it is declared.

Example

Sub Test() As Integer
  Static N As Integer = 1
  N *= 2
  Return N
End

Dim I As Integer
For I = 1 to 10
  Print Test();;
Next
Print
2 4 8 16 32 64 128 256 512 1024

See also