All
simple variables are variant variables. Which means that any
variable can store any type of number or a string. Variables can be
global or local. Local variable values can be used only while their
parent sub is executing. Global variable values can be used
everywhere.
Except
for array variables, there is no need to declare a variable before
using it.
Global
variables are variables that are declared or used outside a Sub in
the main code body, all other variables are local.
To
pass data between Subs you could use global variables, or better,
you can pass the data as parameters. When using parameters the
variable is not passed, but its value is copied. Remember that if
you change a local variables value in one place (even if it had
received its data from a parameter), then the change will not
affect any other variable outside the current sub.
Array Variables
B4Script
supports arrays with up to three dimensions. All global variables
can be Dimmed as multi-dimension arrays of up to three dimensions
as many times as required anywhere in the program. Variable "A()"
or "A" is always equivalent "A(0)" , "A(0, 0)", "A(0, 0,
0)"or "A()". Where an array reference, as opposed to the value of
an array element, is required then A or A() or A(x) are all
accepted. Local variables can never be arrays. Multi-dimensioned
arrays may also be accessed as single dimensioned arrays using the
length returned by ArrayTotalLen or GetTotalLen when an array name
is passed as a parameter.
Although
arrays, as opposed to the value of a single item of an array,
cannot be passed as parameters the same functionality can be
achieved by passing the array name and then using GetItem, GetLen,
GetRank, GetTotalLen and SetItem which provide indirect access to
an array specified at runtime.
Declare
array variables with the "Dim" keyword.
See
"Array" for a convenient method of initializing arrays. The same
array can be declared again with a different size and rank any
number of times during the program.
An
array index starts from 0 and its last index is array dimensions -
1.
Example:
Dim
Books (20)
Dim
Buffer (100)
Result:
Declares an array of 20 items starting from Books(0) and up to
Books(19) and an array of items starting from Buffer(0) and up to
Buffer(99)
When
you need to reference an entire array (not one item in the array)
write the array name alone or followed by ().
Example:
Array(buffer(),1
,2 ,3)
or
Array(buffer,1
,2 ,3)
Structure Variables
Structure
are actually arrays with a convenient access mechanism for the
rightmost dimension of the arrays. They are declared using "Dim
Type" and may have an additional two dimensions specified in
addition to the dimension implied by the indexing
variables.
For
convenience any array may be addressed by the "." convention,
"Array.Variable", where the variable contains the required index
into the array. Variables used in this way may be any local or
global variables. The "Dim Type(...) "statement is provided to
automatically define and initialise variables for this use but any
non-array variable may be used. These indexer variables are local
or global variables, depending upon where the Type statement is
invoked. and whether a global variable of that name already exists.
Once their values are automatically assigned by "Dim Type" care
needs to be taken not to unwittingly assign different values to the
indexer variables unless this is what is really
intended.
See
the help entry for "Dim Type" under "Keywords -> General" for
more information.