Definitions in Classes
- Updated2025-10-31
- 5 minute(s) read
A class represents a self-contained unit that is independent of the test. Therefore, no test variables can be defined in a section globals code block in classes. Test variables can only be used indirectly within a class by transfer parameters to functions of the class or by defining references, as described in the following section. Only class variables can be defined in a class.
Definition of Class Variables
The definition of class variables distinguishes between variables and references. A reference is a reference to an existing PAtools element. References can be compared with pointers in PAtools.
Definition as Variable
The definition of a class variable as a variable is identical to the definition of variables in the program or in functions. For the definition of variables, the keywords private and const can be preceded.
[private] [const ] datatype variableName [ = Initialisation]
If the optional initialization value is omitted, the class variables are automatically initialized with a value that depends on the data type.
Visibility of Variables Outside Classes
All variables that are defined in a class without additional key words, can also be read and written by the program that uses the class. This is not in all cases desired.
As with libraries, you can also annotate variables with the preceded keyword private. Variables labeled this way can only be used within the class. Access to them from outside the class is not possible.
Example
private int32 initValue
Constant Class Variables
If you only need to read a class variable and not change it, you can precede the const keyword when you define these variables. This variable can have assigned a value only once during the definition. Subsequent changes to this value are not possible within the class or when they are used.
Example
const int32 constValue = 1
Definition as Reference
In certain situations, it may be advantageous to directly read or manipulate PAtools variables and value tables. References are offered for this purpose.
In classes, references always have to be initialized in the constructor function that means to reference to a existing variable Then you can use them like normal variables (read and write). In the course of the program, however, a reference has the additional option to set the reference to a different PAtools variable.
The definition of a class variable as a reference is done using the ref keyword.
[private] ref type identifier
- References in classes can be any complex data type (fields, value tables, and objects), regardless of whether they are defined global or local.
- For references the preceded keyword private is allowed, but const is not.
- Of the simple data types (numbers, integers, buffer, PAbasic, and text), only those data types can be used that are defined in PAtools. These have to be defined globally.
- References to buffer and text are specified without the value of the maximum length of the text.
- References to fields and value tables are defined without dimensions and initialization.
Example
ref int32 counter private ref real64 x ref text MsgBoxHeader ref buffer t ref pabasic dynabl.p private ref int32[] intArray ref variant[] AKS_Parameter.WTB
Read and Write References
Reading values from a reference variable and writing values to a reference is analogous to accessing class variables through the = operator.
When you assign a value to a reference variable, the result is that the value of the variable that initialized the reference variable is changed. Any other instance of this class that also holds a reference to this variable will then get the return value that is written to the reference during the assignment when reading.
Example
class className
ref int32 intRef /* definition of the reference as a publicly visible class variable */
function className(ref int32 x, int32 y) // construktor
section references
intRef = ref x // 1. Initialization of the reference
endsection
endfunction
endclass
Program
section globals
int32 intVar1, intVar2, intVar3 /* global variables of the program */
endsection
className myClass (ref intVar1)
myClass:intRef = intVar2 // Changing the value of the referenced variable →
value of intVar1 = value of intVar2 after this call */
intVar3 = myClass:intRef /* Reading the value of the referenced variable →
value of intVar3 = value of intVar2 after this call */
endprogram
Change Referenced Object
You can change the referenced object resp. the referenced variable at run time. This corresponds to moving a pointer. To do so, you must write during the assignment keyword ref before the new source reference.
Destinationreference = ref NewSourcereference
Example 1: Directly Changing the Referenced Variable
class className
ref int32 intRef // Definition of a reference as public visible class variable
function className(ref int32 x, int32 y) // Constructor
section references
intRef =ref x // 1. Initialization of the reference
endsection
endfunction
endclass
Program
section globals
int32 intVar1, intVar2 // Global variables of the program
endsection
className myClass(ref intVar1)
myClass:intRef = ref intVar2 // Directly changing the reference; intRef now
points to intVar2
endprogram
Example 2: Assignment Using a Class Function
class className
ref int32 intRef // Definition of a reference as public visible class variable
function className(ref int32 x, int32 y)// Constructor
section references
intRef =ref x // 1. Initialization of the reference
endsection
endfunction
function changeReference(refint32 x,int32y)// Reinitialization of the reference
section references
intRef = ref x // Reinitialization of the reference
endsection
endfunction
endclass
Program
section globals
int32 intVar1, intVar2 // Global variables of the program
endsection
className myClass(ref intVar1)
myClass:changeReference(ref intVar2) // Changing the reference in the
function; intRef now points to intVar2
endprogram
Definition of Functions
The general syntax for defining functions within a class or within a program or a library is identical.
Also in classes, functions can be provided with preceding keyword private. These functions can only be used within the class, they cannot be accessed from outside the class.
Functions without keyword private can be called from instances of this class.
Example
[private ] function real64 result = doMyPrivateJob (real64 param1)
Constructor
A special case in the definition of functions is the constructor function: calling the constructor function in a program creates a new instance (= a new object) of the corresponding class.
Each class requires a constructor function. This constructor function has to be defined within the PAscript class and has always the exact name of the class. It serves for initializing variables and objects that are used within the class.
Definition of the Constructor Function
Constructor functions have no return value, but they can have transfer parameters.
If references are used, they have to be declared in the constructor function within section references. If a section references is created, this has to be the first part of content in the constructor function, even before the declaration of the local variables.
If no references are used, the section references can be omitted.
class className
ref datatype_n ref_n
function className
(datatype_1 parameter_1, ...,datatype_n parameter_n)
section references
ref_n = ref Parameter_n
endsection
// Definition of local variables
// Implementation code constructor function
endfunction
// implementation code class
endclass
Example
The following example shows different types of parameter transfer in the constructor function.

Related Information
- Initialization Code or Value
The initialization code or value of a simple local variable is optional. If there is no initialization value present, the variable is pre-initialized with 0 or "", depending on the type.
- User-Defined Functions