< Gambas

Back to Gambas

Variable Declaration ( Dimensioning)Edit

In Gambas it is necessary to declare every variable. To dimension a variable, the code is:

Dim variablename as type

You can just skip the word Dim , so the following is also correct:

variablename as type

The variable name could be whatever you want. The type however has to be chosen. You have a choice of integer, string, variant and some others. This tells the computer what type of information the variable holds. Singles and integers hold numbers, while strings hold text.

Dim i as Integer
Dim x as Variant
i = 31             'This is ok
i = "i do"     'Error: type mismatch(i is an integer;"i do" is a string)
x = 341            'This is ok
x = "books"        'This is all right

In Gambas you cannot dim an array as you were used in VB. The following is wrong:

    xxxxx  Dim x(1 to 10) as integer  xxxxxx

Dimensioning can also be used to tell the computer that variables are public or private. When a variable is public, it may be used and changed in all sub-programs. To make a variable public you must dim it in the "declarations" area of the code. These declarations are placed on the very first lines in your code, above all sub programs and other code.

Examples: You need a command button on your form to get it going.

PUBLIC SUB Button1_Click()
x AS integer
y AS float
z AS String
x = 2
y = 2.378
z = "That ist correct"
'the use of variables 
print x,y,z,x*y

The following program will cause an error: x already declared

PUBLIC SUB Button1_Click()
DIM x AS integer
DIM x AS float
DIM x AS String
x = 2
x = 2.378
y = "That ist not correct"
print x,x,x

In the following program you also cause some error warnings, as there is no difference between upcase and lowcase signs in Gambas.

PUBLIC SUB Button1_Click()
DIM x AS integer
DIM X AS float
DIM x$ AS String
x = 2
X = 2.378
x$ = "that ist correct "
print x,X,x$

List of Gambas predefined datatypes for the declaration of variablesEdit

Name Description Memory size Default value
Boolean True or False 1 byte False
Byte 0 ... 255 1 byte 0
Short -32768 ... +32767 2 bytes 0
Integer -2147483648 ... +2147483647 4 bytes 0
Float Like the double datatype in C 8 bytes 0.0
Date Date and time, each stored in an integer . 8 bytes Null
String A reference to a variable length string. 4 bytes Null
Variant Any datatype. 12 bytes Null
Object A anonymous reference to an object. 4 bytes Null


Once you have declared a variable , you can assign a value to it.

Variable = Expression 

This will assign the value of an expression to one of the following elements :

  • A local variable.
  • A function parameter.
  • A global (class) variable.
  • An array slot.
  • An object public variable.
  • An object property.


iVal = 1972
Name = "Gambas"
hObject.Property = iVal
cCollection[sKey] = Name


Some versions of basic, allow structured variables to be created by using TYPE definitions as follows:

' This will not work in gambas
TYPE rhubarbstructure
  foo AS STRING * 32
PUBLIC rhubarb AS rhubarbstructure

Currently, gambas does not support the TYPE keyword. However, it is possible to use class definitions for the same effect.

Creating a classEdit

From the Integrated Development Environment file, create a class file rhubarbstructure.class as follows:


It is now possible to define a variable utilizing that class, by creating an appropriate definition in the code module as follows:

DIM rhubarb AS rhubarbstructure