|
Tables
The use of tables is very common when programming. It consists of indexing a number of variables. You must take care of a few things for their use.
Each element of the table is a variable that you
can use as any other variable of the same type.
These variables are defined one after each other
in the memory.
To declare a table, the syntax is :
Local TableName[NumberElement] as VariableType
|
VariableType is the type of each element of the
table. The "[" and "]" following
the table name allows the compiler to see the difference
between a normal variable and a table. Betweens
that, you have to put the number of elements in
the table. It will be used by the compiler to allocate
enough place to storage the elements in memory.
For a local declaration, the number of elements will be evaluated during execution, so you can use an expression with other variables.
For a public declaration, the number of elements
must be constant.
Example :
Local ProductPrice[5] as Long
|
This line will keep a memory place for 5 variables of Long type.
Using the elements of the table :
To search for an element in the table, you must use an index. The syntax consists in putting brackets after the table's name and to put in these brackets the index, that is the number of the element.
Example :
Important note : Indexes in tables begins
at 0 ! That means that for a table containing 10
elements, the index will be between 0 and 9 (0 and
9 included). The expression TableName[0]
is the first element of the table, TableName[1]
the second, etc. This small thing has important
effects on execution speed. A compiler with a first
indexation of [1] will slower at the execution time.
For speed reasons, you must also know that the number
of elements you use in the table's original declaration
is only used by the compiler for memory space. The
compiler doesn't know if you are taking a value
outside of the table. It means that if you take
the sixth value of a table with five elements, you
won't have an error message. In this case, you erase
the memory after the last element, thing that you
mustn't do.
When a local table is created, a memory space is
given for the variables. But the memory space is
not modified, not initialised, and contains random
values.
If you abolutely need to initialise your table, use a routine like this one :
Local table[10] as Integer
For i=0 To 9
table[i] = 0
Next
|
Public
tables are initialised to 0 value. You don't have
to initialise them.
Defining a multiple-dimensions table
With ETP-Basic, you can define table with multiple
dimensions. There is no limitation for the number
of dimension. To declare a table with two dimensions,
use the following syntax :
Local Tablename[NumberElement1,NumberElement2] as VariableType
|
To use a two-dimensions table, you must put the two indexes.
Example :
Local matrix[10,4] as Integer
matrix[7,3] = ...
... = matrix[7,3]
|
To declare a n-dimensions table :
Local TableName[NumberElement1,NumberElement2, ... , NumberElementN] as VariableType
|
|