What is a Variable?
A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running. For example, you might create a variable called Count to store the number of files present into a directory. Where the variable is stored in computer memory is unimportant. What's important is that you only have to refer to a variable by name to see its value or to change its value.
thinBasic supports 4 main variable types:
For each of them there are many numeric and string subtypes.
Every variable you need to use in your script must be declared and its type specified.
You declare variables explicitly in your script using one of the following statements: Dim, Local, Global, Static.
Dim Count As Long
Dim OutFileName As String
Multiple variables can be declared by separating each variable name with a comma. For example:
Dim Count, Idx As Long
Variable names follow the standard rules for naming anything in thinBasic. A variable name:
|•||Must begin with an alphabetic character.|
|•||Cannot contain embedded period, space and, in general, punctuation.|
|•||Must not exceed 64 characters.|
|•||Must be unique in the scope in which it is declared.|
Scope and lifetime of Variables
A variable's scope is determined by where you declare it. When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is called a procedure-level variable.
If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable or global variable, and it has script-level scope.
How long a variable exists is its lifetime. The lifetime of a script-level variable extends from the time it's declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed.
Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.
Scalar variables and Array variables
Much of the time, you just want to assign a single value to a variable you've declared. A variable containing a single value is a scalar variable.
Other times, it's convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable.
Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name.
In the following example, a single-dimension array containing 10 elements is declared:
Dim A(10) As Long
Important: all arrays in thinBasic are one-based. Other languages are zero-based so the above declaration would actually contains 11 elements while in thinBasic contains 10 elements.
You assign data to each of the elements of the array using an index into the array. Beginning at one and ending at 10, data can be assigned to the elements of an array as follows:
A(1) = 256
A(2) = 324
A(3) = 100
A(10) = 55
Assignment can also be done in multi assign way. For example to assign the first 3 elements a value, something like the following is perfectly valid:
A(1) = 256, 324, 100 '---Will assign 3 values starting from position 1
A(5) = 256, 324, 100 '---Will assign 3 values starting from position 5
Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example:
SomeVar = A(1)
Arrays aren't limited to a single dimension. You can have as many as 3 dimensions, although most people can't comprehend more than three or four dimensions. Multiple dimensions are declared by separating an array's size numbers in the parentheses with commas.
In the following example, the MyTable variable is a two-dimensional array consisting of 5 rows and 10 columns:
Dim MyTable(5, 10) As Long
In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.
You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using the Dim statement. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.
Dim MyArray() As Long
To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.
In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.
Dim MyArray() As Long
There is no limit to the number of times you can resize a dynamic array, but you should know that if you make an array smaller than it was, you lose the data in the eliminated elements.