Variable Declarations

Variable names can be mixed case (upper and lower case characters), but names are not case sensitive, i.e. Abc, ABC, abc, aBC all refer to the same variable. 

Within a given context, variable names must be unique even if they refer to variables of different data types and variable names cannot match system keywords.  For example, you cannot have a string variable named “value1” and an integer variable with the same name.  System keywords generally refer to words such as “For”, “If”, “Dim” that are expected to denote a built-in language capability.

Variable names must start with either a letter or an underscore “_”.  This character can be followed by a sequence of up to 127 additional letters, numbers, and underscore characters for a total of 128.  If a variable name starts with “_” it must be followed by at least one other character other than another underscore to distinguish it from a line-continuation.

Dim is the basic data type declaration statement within procedures for local, i.e. automatic, variables.  If Static is used in place of Dim within a procedure, the value of the variable is preserved from one execution of the procedure to the next.  Dim variables, including array variables, are initialized to 0 (numbers), False (Booleans), or Nothing (structures, objects, or classes), each time their enclosing procedure is executed.

Dim ii As Short
Static
jj As Short

Variables defined within a module, outside of a procedure, are accessible by all procedures in the module and, like Static variables, their values are preserved independently of the execution of any procedure. If such variables are defined with Private or Dim, the variables are local to the module and cannot be accessed by procedures in other modules. If Public is used instead to declare a variable, the variable is accessible by all procedures within all modules loaded into the controller’s memory.

Module Test
Dim Count As Integer      ' Invisible to other modules,
' global in this module
Private nBlocks As Integer ' Same as declaration above
Public TotalArea As Single ' Visible to all procedures in
' all modules within project.
End Module

Variables declared within a module can also be accessed by preceding the variable name with the module name. This method of specifying a variable is required for cases when the same Public variable name is found in more than one module and it is unclear from the name alone which variable is being referenced.

Module Test1
Public aa As Integer
Public bb As Integer
End Module

Module Test2
Public aa As Integer
End Module

Module Test3
Sub MyProc
Dim ii As Integer
ii = bb ' Okay since there is only bb
ii = Test1.bb ' Okay but not necessary
ii = aa ' Error since aa is duplicated
ii = Test1.aa ' Okay since it is clear which aa
End Sub
End Module

In GPL, no matter where a variable is declared in a procedure, the scope of a variable extends throughout the procedure with the restriction that variables can only be declared in the outermost level of a procedure.

For ii = 1 To 10
Dim jj As Integer ' Not allowed
kk = ii ' Forward reference to kk is allowed.
Next ii

Dim kk As Integer

In the future, we may change the scoping rules to follow other variants of Basic, such as VB.NET, more closely.

Multiple declaration clauses may appear in a single statement.

Dim n As Integer, x As Double

The data type must always be specified.

Dim BlackObject1            ' Invalid

If multiple variables are declared within a single statement and a variable’s type is not specified, its type is defined by the next type definition in the statement [this is different from VB6 where all untyped variables became Variants].  Note, if a New clause (see below) is used, only a single variable name may appear to the left of the As keyword.

Dim ii, jj As Integer' Both ii and jj are of type Integer

Variable or constant values may be initialized by adding an initialization clause that beings with an “=”. For example,

Dim Count As Integer = 1    ' Sets Count to 1

Each time this statement is encountered during execution, its value is initialized. If an initializer clause is used, only a single variable name may appear to the left of the As keyword.

An arbitrary expression may appear to the right of the “=”. If the variable being initialized is an object or structure, a New keyword may appear to the right of the “=”.

Be careful if you call a user-defined function as part of the initializer expression since some variables may not be initialized yet.

Module-level variables are initialized once when a project is started and are processed in the order in which they appear in the module. They are initialized before any user-defined procedures are executed (except in the case where you call a user-defined function from an initializer). Errors that occur while initializing variables are listed as part of a hidden procedure named "_Init".

The New keyword may appear in a clause that declares an object or structure.  The New may appear immediately after the As keyword, or may appear immediately after the “=” in an initialization clause.  New may not appear in both places within the same statement.

Dim Loc1 As New Location     ' Creates a location
' class instance
Dim Loc1 As Location = New Location ' Equivalent to above

A Const keyword indicates that the variable is read-only and cannot be changed during normal execution.  Only the initialization clause can set the value of the Const keyword.

Const MaxCount As Integer = 10
MaxCount = MaxCount+1 ' Invalid

GPL only supports strong typing, i.e. all variables must be declared in a Dim, Static, Private, or Public statement although the specific type of a variable may be excluded and will be automatically set to the default.  [VB.Net allows strong typing to be disabled with the “Option Strict Off” statement.]