Variables
Previous Top Next

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 variables value can be used only while their parent sub is executing.
Global variables’ values can be used everywhere.
Except for array and structure variables, there is no need to declare a local variable before using it.
Since version 5.0 an optional (and recommended) check is done to make sure that all local variables are used and that no variable is used before it is assigned any value.
Global variables are variables that were declared (or used) in Sub Globals; all other variables are local.

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.


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.