Download XPert Basic SLL User Manual

Transcript
Bad_string_example = "all this data
Cannot extend beyond one line"
Good_string_example = "Unless the data " &
"is seperated by &, +, etc."
Variables defined in the main body of the program can be accessed by all the code in the main
body that comes after the definition, including subroutines. Variables defined inside of
subroutines are "local" to that subroutine. That means they are only known to that subroutine,
and any memory they consume is released when the subroutine completes.
A local variable may be declared without supplying an initial value with the Dim statement. The
Dim statement is often used to declare local variables inside of scheduled programs and block
programs because in these special cases the value of local variables are retained across calls, and
the Dim statement will declare but not re-initialization the variable.
Dim LocalVar1
Dim LocalVar2
Basic also supports global variables. Global variables may be accessed from separate programs
(.bas files), as well as among from different subroutines and functions. They are defined and
optionally initialized with the Static statement. If a Static variable is not explicitly initialized it
will start with a value of 0. This can be handy if you wish to retain the value of a variable even
after recording has been restarted.
Static GlobalVar
Lock
If GlobalVar = 0 Then
GlobalVar = 1
StatusMsg "Program was just loaded"
End If
UnLock
Note the use of the Lock and UnLock statements in the above example. These statements ensure
no other program can access the variable GlobalVar while the code between the statements is
executing. If a global variable can be accessed from different thread contexts, i.e., from different
Basic Blocks, Sensor Blocks, and Scheduled Programs, then access to the variable must be
protected, as shown.
Basic supports single dimensional and multi-dimensional arrays. They are dynamically sized
much like strings. If you need a really large array, you might initialize its highest index first so
less time is wasted expanding the array.
TestArray(5)=5
TestArray(0)=0
TestArray(1)=1
TestArray(2)=2
TestArray(3)=3
TestArray(4)=4
or…
TestArray = Array(0, 1, 2, 3, 4, 5)
Array indices inherently start at 0, although you may use 1 as the base index for clarity (negative
indices are not allowed).
20