(x) on the variable name. X number of variables are created that differ in name
only by the incrementing number in the suffix. For example, the four statements
Public TempC1
Public TempC2
Public TempC3
Public TempC4
can simply be condensed to
Public TempC(4).
This statement creates in memory the four variables TempC(1), TempC(2),
TempC(3), and TempC(4).
A variable array is useful in program operations that affect many variables in the
same way. CRBasic example Using a Variable Array in Calculations
(p. 136)
shows compact code that converts four temperatures (°C) to °F.
In this example, a For/Next structure with an incrementing variable is used to
specify which elements of the array will have the logical operation applied to
them. The CRBasic For/Next function will only operate on array elements that
are clearly specified and ignore the rest. If an array element is not specifically
referenced, as is the case in the declaration
Dim TempC()
CRBasic references only the first element of the array, TempC(1).
See CRBasic example Concatenation of Numbers and Strings
(p. 284) for an
example of using the += assignment operator
(p. 565) when working with arrays.
CRBasic Example 6. Using a Variable Array in Calculations
'This program example demonstrates the use of a variable array to reduce code. In this
'example, two variable arrays are used to convert four temperature measurements from
'degree C to degrees F.
Public TempC(4)
Public TempF(4)
Dim T
BeginProg
Scan(1,Sec,0,0)
Therm107(TempC(),1,1,Vx1,0,250,1.0,0)
Therm107(TempC(),1,2,Vx1,0,250,1.0,0)
Therm107(TempC(),1,3,Vx1,0,250,1.0,0)
Therm107(TempC(),1,4,Vx1,0,250,1.0,0)
For T = 1 To 4
TempF(T) = TempC(T) * 1.8 + 32
Next T
NextScan
7.8.4.5 Declaring Local and Global Variables
Advanced programs may use subroutines (p. 288) or functions (p. 602), each of which
can have a set of Dim variables dedicated to that subroutine or function. These
136