Type Class
A class is a template for creating objects, which could be seen as object constructors. Objects are instances of the class from which variables and routines can be defined.
The following example shows an example of a local class with two objects:
type
car : class // creates the class "car"
public
manufacturer : alpha 20
model : alpha 20
primary_color : string
end_class
var
car1 : car
car2 : car
end_definitions
routine main
begin_routine
car1 := car.create () // first object in the class "car"
car1.manufacturer := 'Red Speedy Cars'
car1.model := 'Cavallo Veloce'
car1.primary_color := 'Red'
car2 := car.create () // second object in the class "car"
car2.manufacturer := 'Life-Sized Toy Cars'
car2.model := 'Viento Rápido'
car2.primary_color := 'Aged Copper'
end_routine
Supported Data Types in classes are; alpha, boolean, class, financial, integer, list, numeric, realstring, signed, string, and structure*.
*It is recommend to use nested classes instead of structures in classes. Structures in classes can not be used in calls from or to web services. In addition, when using the <Class>.ToJson or <Class>.ToXml functions on a class which contains a structure, the items in the structure will be serialized to a JSON string or XML string respectively but the structure itself will not.
Global and Local Classes
Classes can be defined as either global, which can be accessed in any form, report, and global
routine in the application, or as local inside its form, report or global routine. Local classes can
only be accessed from inside the object where they are defined.
- Global Class Definition: A global class must be created in a dedicated class object. This process is described on the Classes pages.
- Local Class Definition: A local class with its variables can be created in the definitions
section under the type keyword of a form, report, or global routine. This keyword is not added
automatically to the definitions section and must be added manually.
begin_definitions
type
car : class
public
manufacturer : alpha20
model : alpha20
end_class
var
end_definitions
Access Modifiers
Class members (often individually referred to as fields and methods) are the variables and routines part of the class. These class members can be defined with either a public or private access modifier. Members with a private access modifier can only be accessed from within its own class, whereas members with a public modifier can also be accessed outside its class.
An access modifier is added before defining the variables as shown in the following image:
Class Routines
In addition to variables, class routines can be defined as members that are part of a class.
Here is an example of a class routine:
var
electric_windows : boolean
aircon : boolean
begin_routine
if luxury = true
electric_windows := true
aircon := true
endif
end_routine
Also see the page Class ROUTINE for additional information.
Class Create Routine
It is optionally also possible to add a custom create routine (also known as a class constructor), which becomes active when an instance of the class is created. These too, like a class described earlier, can be either global or local must have a public access modifier. One benefit of this routine is that values can be assigned to variables before a new instance of the class is created. Parameters can optionally be added to the created routine.
Here is an example of a class create routine:
begin_routine
manufacturer := 'Speedy Red Cars' // defines the variable before the object is created
model := 'Cavallo Veloce'
end_routine
routine main
begin_routine
car1 := car.create ()
car1.primary_color := 'Red' // "assigns a value to the variable after the object is created"
sme ('Manufacturer: ' + car1.manufacturer + ' | Model: ' + car1.model + ' | Color: ' + car1.primary_color)
end_routine
The resulting SME will show: "Manufacturer: Speedy Red Cars | Model: Cavallo Veloce | Color: Red".
Class Compare Routine
A class compare routine is a routine that makes it possible to compare two instances of the same class. To begin comparing a class, it must first be declared with the keywords implements icomparer. For example:
type
car : class implements icomparer
public
manufacturer : alpha 20
model : alpha 20
primary_color : alpha 20
amount_doors : numeric 1
end_class
Inside the public routine a two-way comparison must be made that returns one of three results—either 1, 0, or -1—depending on whether the value of a class member from one instance is greater than, equal to, or less than the value of the same class member from another instance.
In the example below, two class instances from the class car have their variable "amount_doors" values compared in a routine.
begin_routine
if A.amount_doors < B.amount_doors
result := -1
elseif A.amount_doors > B.amount_doors
result := 1
else
result := 0
endif
end_routine
A <class>.compare routine is required to sort the instances in a list of classes. This routine is also required to match two instances to find the IndexOf from a class instance in a list of classes. Just like in the image above, when a sort function is used on a list of this class, the instances will be sorted ascending with the values available in "car". To sort the list descending, the operator signs can be switched around.
Things to keep in mind:
|
A report with a complete example of a List of Classes using the Compare routine is available here: List of Classes Comparer example.
Chained Classes
Chained classes are classes that make a call to retrieve the result of another class' method or use one of its properties.
The following example demonstrates this usage:
begin_routine
car1 := car.create ()
car1.get_feature ().aircon := true
sme (car1.get_feature ().aircon)
end_routine
public routine car.create ()
begin_routine
luxury := features.create ()
end_routine
public routine car.get_feature () : features
begin_routine
result := luxury
end_routine
To further explain what is happening in the example given above; the instance car1 from the
class car is accessing the class features (through a routine called
get_feature) to retrieve the property aircon and assigns the
value true to it.
Subjects