Creating
a gambas2
program,
step by step, a telephone index
From
: http://listingambas.blogspot.com/2011/06/anexo-3-introduccion-al-gambas-tipo-de.html
Annex 3: Introduction to Gambas. Variables and data type:
A variable
is an element that is used to store different data types, such as
numbers, letters or both.
The value stored in the variable may change at any time during the
execution of a program, unlike the constant where the value is
maintained unchanged.
Basically there are two places where
you can declare variables in Gambas, depending on what usage you
want for them.
They can be declared within a subroutine or a function, this
statement will only provide use of these variables within that
subroutine or function
If declared in initial part, either in a module or in a class,
variables will be available for that file in all its functions or
subroutines .
To use a variable in Gambas we have to declare it, and for this there
are several ways.
Declaration of local variables
[DIM] ID AS Datatype
With this syntax we
declare a variable within a
procedure or a function, this variable is only accessible in
the function or procedure where it was
declared.
Declaring
an integer variable "val"
DIM Val AS Integer
Declaration
of a string variable "NAME"
DIM NAME AS String
Declaration
of an array, a three by three "Matrix" for float (floating
point numbers)
DIM Array [3,3] AS Float
Declaring
a variable "nObject" with type Object
DIM nObject AS Object
[STATIC] (PUBLIC |
PRIVATE) Identifier [Array Declaration] AS [NEW] Datatype
This
syntax is used to declare a global variable in a class, the declared
variable is accessible from anywhere in the class or module where
it was declared.
If
the statement is preceded by the word PUBLIC, the variable will
be accessible from other classes that reference a object of the class
where the variable was declared.
If
we use STATIC, variable will be shared by all objects of the
class. If you include the word NEW, the variable is
initialized with a new instance of the class specified with datatype.
Subroutines or functions
Subroutine: A procedure
that runs something but returns no value. They are identified in Gambas by the
reserved keyword SUB and do not have the
statement RETURN.
Function: This procedure returns something, is
identified by the keyword FUNCTION and has RETURN.
Data Types.
The
data types used in Gambas are the
following:
Boolean:
supports two values (true) and false (false)
Byte:
admits integer values between 0 and 255
Short:
supports integer values from -32768 to +32767
Integer:
supports integer values between -2147483648 and +2147483647
Float:
supports floating point values,
equivalent to double in C .
Date:
supports values of type date and time. Internally the date and
time are stored in UTC format.
String: Used to
store a text string. supports
values that includes both letters and numbers
Variant: supports any value
Object: to declare variables that reference an object
Determine which type of data
store a variable.
IsBoolean
(expression): returns TRUE if the
expression is a Boolean value
IsDate
(expression): returns TRUE if the
expression stores a DATE value
IsFloat
(expression): returns TRUE if the
expression data type stores a floating point value
Isinteg
(expression): returns TRUE if the
expression stores an integer data type
IsNull
(expression): returns TRUE if the
expression is NULL
IsNumber
(expression): returns TRUE if the
expression data type stores a number
IsObject
(expression): returns TRUE if the
expression stores an object or a null reference
IsShort
(expression): returns TRUE if the
expression data type stores a short value
IsString
(expression): returns TRUE if the
expression data type stores a string
Converting data types
Gambas have
these functions
for conversion of different data types:
1. CBool (expression)
Converts expression into a boolean value.
Example CBool (expression) will return
'true' or 'false'.
2. CShort (expression), Cint (expression), Cinteger
(expression), CLONG (expression) converts the expression into short,
int or long value.
3. CDate (expression) Converts an expression into a value
4. CStr (expression) Converts an expression into a
string.
5. Val (expression) Converts a string into a boolean,
number or date, according to the contents of the string.
6. Str $ (expression) Converts the expression into a
string.