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

By default, variables do not have to be declared before they can be used. You can 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).

 

Optionally, implicit declaration can be disabled using the Explicit option (see the SetOption for details). If implicit declaration is disabled, all variables must be explicitly declared before they can be used.

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 uses three types of variants: long integers, doubles (8-byte floating point numbers) 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, 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 the appropriate type before the expression is evaluated.