Regular variables
Regular variables are variables that hold a single value (unlike arrays or structures
which hold multiple values).
Variables can be either global or sub local.
Local variables value can be used only while their parent sub is executing.
Global variables’ values can be used everywhere.
Global variables are variables that were declared (or used) in Sub Globals; all other
variables are local.
As of v6.90 it is possible to declare the type of regular variables. The two main types
are String and Number. By default all variables are Strings. Number type corresponds
to .Net Double type which stores floating point values.
Other types available are Integer which holds whole numbers and Boolean which
holds the values True and False.
Basic4ppc tries to convert numbers, strings and booleans values when required.
However it is highly recommended to declare numeric values as Numbers as
mathematical calculations done with these variable are much faster (in the compiled
code) compared to such calculations with String variables.
For example, the following code will work properly:
Dim s As String
Dim n As number
n = "42"
s = n & "1"
n = s 'n = 421
It is possible to use regular variables without any declaration. These variables will be
of type String (which can hold all values).
An optional check is done for undeclared variables to make sure that each variable is
assigned a value before its value is read and that each variable is eventually used.
This check can be disabled by unchecking Tools - Check for unassigned / unused
variables.
See Modules for more information about global variables access modifiers.
Example:
(An error message will show if using the check unassigned variables.)
Sub Globals
a=20
End Sub
Sub App_Start
b=10
CalcVars
End Sub
Sub CalcVars
Msgbox ("a = " & a)
Msgbox ("b = " & b)
End Sub
Result: First msgbox will show a = 20
Second msgbox will show b =
b is empty because it's local and wasn't assigned any value yet in this sub.
To pass data between subs you could use global variables, or better, you can pass the
data as parameters.
Example:
Sub Globals
a=20
End Sub
Sub App_Start
c=10
CalcVars (c)
End Sub
Sub CalcVars (b)
Msgbox ("a = " & a)
Msgbox ("b = " & b)
End Sub
Result: The second msgbox will show b = 10
Note: When using parameters the variable is not passed, but its value is copied.
In the above example, I used a variable named c instead of b.
Remember that if you change a local variable’s value in one place (even if it had
received its data from a parameter), then the change will not affect any other local or
global variable outside the current sub. That unless the parameter is declared with
ByRef.
Array Variables
Basic4ppc supports arrays of up to three dimensions.
Array variables are always global and must be declared before used.
Arrays can store items of a certain data type.
This is useful for working with external libraries which sometimes expect an array of
a certain type.
Declare global 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 any number of times
during the program.
However, it must be first declared in Sub Globals (even with 0 items).
An array index starts from 0 and its last index is array dimensions - 1.
Example:
Sub Globals
Dim Books (20)
Dim Buffer (100) As Byte
End Sub
Result: Declares an array of 20 items starting from Books(0) and up to Books(19) and
an array of bytes 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's name followed by ().
Example:
i = ArrayLen (buffer() )
BinaryFile.WriteBytes (buffer())
Arrays can be declared with 0 items like:
Dim Buffer(0) As Byte
Buffer is an empty array that can be used later with an external library which returns
an array.
Example:
Buffer() = Serial.InputArray
Structure Variables
Structures are variables with customized fields.
Using structures the code can be clearer and better organized.
Structures must be declared in Sub Globals.
See the Dim keyword for information on declaring structures. See Array for a convenient method of initializing structures. Using regular structures is similar to using a control's properties.
Write the name of the variable followed by a period '.' and a list of the available fields
will pop.
Example:
Sub Globals
Dim Type(Name, ID, Age) person
End Sub
Sub App_Start
person.Name = "John"
person.ID = 1234567
person.Age = 30
End Sub
You could also use arrays of structures (up to two dimensions):
Sub Globals
Dim Type(Name, ID, Age) persons (100)
End Sub
Sub App_Start
persons(0).Name = "John"
persons(0).ID = 1234567
persons(0).Age = 30
End Sub
Notes:
- On the device only, you need to press on Subs - REFRESH in order that any
declaration change will appear in the pop up list.
- You can reference the whole structure using the structure name followed by ().
Example: person() = SomeSub
- Structures are converted to arrays during the compilation.
In the first example, person will be an array of one dimension, person.Name = "John"
will be converted to person (0) = "John" and so on.
You can also use the array syntax to work with structures.