Class - Defining a Class

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Classes

Functions Subroutines Classes
Defining a Function Predefined Functions Tag Functions Mathematical Functions String Functions Defining a Subroutine Trigger Subroutines Defining a Class

Related Links: User Defined Class and Functions, Example PGM Files

Class - Introduction Class - Defining a Class Class - Using a Class Class - Macros Class - Examples

Global Predefined Class Instances: Species Database Class, Particle Size Definition Class, Plant Model Class
Predefined Classes: Array Class, StrArray Class, Matrix Class, TagSelect Class, Noise Class, TimeClass


How to Define a Class

Class className

;Variable declaration(s)

;Sub(s) and Function(s)

EndClass
  • Class: The class declaration must begin with the Class keyword
  • EndClass: The class declaration must end with the EndClass keyword.
  • className: A globally unique class type name must be provided. This is later used as a "data type" when declaring instances of classes of this class type. See How to use the Class.
  • TextLabel can be used within the class for arranging display of variables in the Access window.
  • ClassAsPageLabel keyword can be used to automatically create tab pages in the Access window based on class instance name.
  • Note that Class definitions cannot be nested (i.e. classes cannot be defined within the definition of a class).
  • Various PGM language commands are unavailable within a class definition.

Syntax for Class Declaration

Function Function Syntax Description/Notes/Example
Declaring a Class
Class ClassName
;Class code
EndClass

Class: The class declaration must begin with the word Class
ClassName: A globally unique class type name must be provided. This is later used as a "data type" when declaring instances of classes of this class type.
EndClass: The class declaration must end with the word EndClass

Class Variable Declaration For Variable Declaration Syntax,
refer Variable declaration.
Variables can be declared within the class, their scope is limited to the class, and they are available to the instance of the class.
  • When declaring variables in a class, standard variable declaration rules apply. This includes adding engineering units and appending * or @ to make the variable visible in the access window.
  • Users cannot define another class within a class (nested classes). However, variables of type class can be declared within a class. These may be predefined classes (e.g., Matrix Class) or user-defined classes.
  • Enum DropList and DropList cannot be defined within a class. Predefined drop lists outside of the class can be used within the class. See Example : using Enum Drop list and Drop List in a class.
Syntax for Subs and Functions
Sub SubroutineName()
Function FunctionName()
StrFunction StrFunctionName()
Any number of functions (of type Sub, Function and StrFunction) can be declared within the class and they are available to the instance of the class. The sequence of functions and variables within a class can be mixed and arranged as required.

Typically, a class will have these three types of subroutine in it:

  • Initialise subroutine
  • Calculation subroutine(s)
  • Execute subroutine

Special Functions

ClassName() This Special Function is available for use within class functions and subroutines. ClassName returns a string of the class type name. In example below, this will return "CSTR_Class".
ClassTag() This Special Function is available for use within class functions and subroutines. ClassTag returns a string of the class instance variable name (tag). This is a particularly useful function when the class instance variable name matches a tag in the model.

For example, we can use UnitTag = ClassTag() within an Init() Sub for the class. See Class Declaration Example

Special Functions -

General Keywords

ClassAsGroup If this keyword is included within the class definition, then later when class instances are declared they are automatically displayed as a group in the Access window.
ClassAsPageLabel Available from Build 139.31388. If this keyword is included within the class definition, then later when class instances are declared they are automatically displayed on a new tab page in the Access window, with the page name matching the class instance name. The user does not need to specify PageLabel(xxx) for each class. It is also useful for each class instance in an array of classes appearing on a new tab page.
ClassAsTextLabel If this keyword is included within the class definition, then later when class instances are declared they are automatically displayed with a text label (heading) in the Access window, with the text label matching the class instance name.
Specal Functions -

Grid Display Keywords

ClassGridColumnWidth Width Use this keyword to change the width of each column displayed in a grid class.
ClassGridMaxColumns MaxColumns Use this keyword to change the maximum number of columns allowed before a new grid is automatically created.
ClassGridHideComment Use this keyword to hide the display of class instance comments that are normally displayed in the first row of the grid.
ClassGridHideText Use ths keyword to hide the display of all TextLabels shown a text between rows in the grid.
Alternate Special Functions - Used outside of the Class definition. As Grid Display Keywords. See Class Variable Display Options for more information and display examples.
ClassAsPageLabel | AsPageLabel | pl
ClassAsTextLabel | AsTextLabel | tl
ClassAsGroup | AsGroup | g
ClassGridColumnWidth | ColumnWidth | Width | cw
ClassGridMaxColumns | MaxColumns | mc
ClassGridHideComment | HideComment | hc
ClassGridHideText | HideText | ht

Special functions (as mentioned in the previous two items) can be defined anywhere within the class definition. If users prefer to use different formats for different class instances, these can be defined outside of the class during the class instance declaration step. The keywords listed are available in (Full | long | short | Condensed) formats, and any format can be used. Simply include them within {} when defining the class instances. For example, using the condensed version of the keywords:
class TankClass{g,pl,cw(12),mc(6)}

Class Declaration Example

  • A very condensed extract of the CSTR_Class PGM file is presented below to explain the structure and use of the Class definition:
Class CSTR_Class
 ClassAsPageLabel           ;example of Special Keyword
 ;... Various Variable Definitions.....
 String     UnitTag{tag}@
 Real       TankVolume*("Vol","m^3")<<300>>
 ;... Various Variable Definitions.....

 Sub Init()
   UnitTag = ClassTag()     ; example of Special Function
   [concatenate(UnitTag, ".ResTime.Volume (m^3)")] = TankVolume
 EndSub
 
 Sub CalculateCSTR()
  ;......Various Calculation Code .....
 EndSub   
 
 Sub Exec()
   ;......Various Calculation Code .....
   CalculateCSTR()                           ;Example of calling a predefined subroutine within a subroutine.
   ;......Various Calculation Code .....
 EndSub
EndClass
  1. The name of the Class (or template) is called CSTR_Class.
  2. All the variables that are associated with the CSTR_Class are defined. (The full PGM file is available in the Gold distributed example project)
  3. Nested functions are allowed within a class, for example Sub "CalculateCSTR" is called inside the Sub "Exec()"
  4. In summary, the Class(template) "CSTR_Class" contains a number of calculations and a number of Set Value Commands.