Mike2423
(Fresh Scripter)
2002-06-24 07:23 PM
Declare variables?

Hi, I'm new to KiXtart, and I'm trying to figure out if you need to declare variables or not. I notice there's a DIM command, but in all the scripts I've seen, no one seems to declare variables - they just use them!

Thanks!


Howard Bullock
(KiX Supporter)
2002-06-24 07:38 PM
Re: Declare variables?

You can do either. See the manual:

Dynamic
Program Variables


Introduction


In KiXtart, variables are used to temporarily store values
during the execution of a script. Variables have a name (the word you use to
refer to the value the variable contains) a type (which determines the kind of
data the variable can store) and a scope (which determines where in the script
you can reference the variable). You can think of a variable as a placeholder in
memory for an unknown value.

Storing data in variables


Variables can be assigned a particular value by using an
assignment statement:

 $Variable = 10


 or by using a GET or GETS statement :

 GET $Variable


 


Optionally, variables can be created and assigned a value
on the commandline with which KiXtart is started. To do this, type the variable
name followed by an equal sign (=) and the value the variable should have. For
example:

 


KIX32
Demo.kix $Key=Value


 



 









Note



On the commandline, do not include spaces between the equal sign
(=) and the value. If you want to specify a value that contains spaces, enclose
it in quotation marks (for example, KIX32
Demo.kix $Key="Hi there"
).


 



Declaring Variables


To declare a variable is to tell the program about it in
advance. You declare a variable with the Dim or the Global statement, supplying
a name for the variable:

 


DIM variablename


 


Variables declared with the Dim statement exist only as
long as the script segment in which the Dim statement was used is executing.
When the script segment completes, the variable, and its value, disappear.
Variables declared with the Global statement exist during the entire KiXtart
session.

 A variable name:

·        
Can't contain operator characters (+,-,*,/,&,<,>,=)

·        
Can’t contain a period

·        
Must be unique within the same scope, which is the range from which the variable can be referenced
in a script, or script segment.

 



 









Note



You can use the same name for variables in different
scopes, and if you do, you will only be able to reference the variable in the
current scope. Please see the example below for more details:

 $Var
= 10


IF
InGroup( “Admins” )


  
DIM $Var            
; local variable with same name


  
$Var = 20


  
? $Var              
; this will display ‘20’


ENDIF


? $Var     
; this will display ‘10’


 



Implicit declaration


Variables don’t have to be declared before they can be
used. You can also implicitly declare them simply by assigning a value to them.
Note that all variables that are declared in this way have a global scope (see
below for details on scope).

Scope of variables


Depending on how and where they are declared, variables can
have a local or a global scope. Variables with a global scope are visible
anywhere in any script during the entire KiXtart session. Variables with a local
scope are only visible to the script or script segment in which they were
created.

 


Examples:

 




















 


$GlobalVariable
= 10




Assuming this is the first reference to ‘$GlobalVariable’,
this variable is implicitly declared and will become a global variable,
visible everywhere in every script during this KiXtart session.


 


DIM $LocalVariable


 


$LocalVariable
= 10


 



This variable will become a local variable and will
be visible only in the current script.


 


IF $X = 1


 


  
DIM $LocalVariable


 


  
$LocalVariable = 10


 


ENDIF


 



In
this example, $LocalVariable will only be visible inside the IF statement.



 


GOSUB Demo


 


EXIT 1


 


 


:Demo


 


DIM $LocalVariable


 


$LocalVariable
= 10


 


RETURN


 



In
this example, $LocalVariable will only be visible inside the subroutine
‘Demo’.



Variable types


In KiXtart, variables are always of one fundamental data
type: Variant. The current implementation of KiXtart only uses two types of
variants: long integers and strings. A variant of type string can contain up to
32,000 characters. Integer variables can contain any value between 2,147,483,648
and 2,147,483,647. The type of a variable is automatically changed to the result
of the expression that is assigned to it. This means that if you assign a string
to a variable containing an integer, the type of the variable is changed to a
string.

 


There is no limit to the number of variables that can be
defined, other than the amount of memory available to KiXtart.

 


Note that KiXtart can handle other types of variants, such
as Booleans, Bytes, etc. KiXtart itself does not create these types itself, but
they may be returned by other programs via COM automation. If any of these types
of variants is used in an expression, it will be converted to a long integer or
string before the expression is evaluated.

Arrays


KiXtart supports single dimension arrays. Arrays allow you
to refer to a series of variables by the same name and to use a number (an
index) to tell them apart. This helps you create smaller and simpler code in
many situations, because you can set up loops that deal efficiently with any
number of cases by using the index number. Arrays have both upper and lower
bounds, and the elements of the array are contiguous within those bounds.
Because KiXtart allocates space for each index number, avoid declaring an array
larger than necessary.

 


When declaring an array, follow the array name by the upper
bound in square brackets. The upper bound cannot exceed 2,147,483,647.

 Examples:

Dim
$Counters[14]      
explicit declaration


Dim
$Sums[20]          
‘ explicit declaration


$NewArray
= 10,20,30    ‘ implicit
declaration


$Array
= “A1”,“B2”,“C3” ‘ implicit
declaration


 The
first declaration creates an array with 15 elements, with index numbers running
from 0 to 14. The second creates an array with 21 elements, with index numbers
running from 0 to 20. The third and fourth declarations create arrays with 3
elements, with index numbers running from 0 to 2.

 Arrays are always of type variant.


 









Note



Unlike regular variables, arrays can not be used inside
strings and can also not be assigned a value on the commandline.

[ 24 June 2002, 19:38: Message edited by: Howard Bullock ]

Mike2423
(Fresh Scripter)
2002-06-24 07:47 PM
Re: Declare variables?

Thanks