Class Declaration

Help Contents

Introduction

A Class Declaration defines a set of variables and methods. A class encapsulates data and associated logic in one declaration. Using Class Declarations you can write object oriented IEC 61131-3 code.

Syntax

A Class Declaration uses this syntax:

class-declaration ::=
  CLASS class-name
    { variable-declaration }
    { method-declaration }
  END_CLASS

method-declaration ::=
  METHOD method-name [ : method-return-type ]
    { variable-declaration }
    [ method-code ]
  END_METHOD

method-return-type ::= [ Elementary Data Type | Derived Data Type ]

method-code ::= [ structured-text | instruction-list ]

Where:

Examples

  1. A class to represent a color using red, green and blue components.
    CLASS ColorValue
        VAR Red, Green, Blue : REAL; END_VAR
    
        METHOD PUBLIC SetRGB
            VAR_INPUT newRed, newGreen, newBlue : REAL; END_VAR
    
            Red = newRed;
            Green = newGreen;
            Blue = newBlue;
        END_METHOD
    
        METHOD PUBLIC GetRed : REAL
    
            GetRed := Red;
        END_METHOD
    
        METHOD PUBLIC GetGreen : REAL
    
            GetGreen := Green;
        END_METHOD
    
        METHOD PUBLIC GetBlue : REAL
    
            GetBlue := Blue;
        END_METHOD
    
        METHOD PUBLIC GetLuminance : REAL
    
            GetLuminance := Red * REAL#0.213 + Green * REAL#0.715 + Blue * REAL#0.072;
        END_METHOD
    
    END_CLASS
  2. To declare an initialized instance of the ColorValue class use:
    VAR StateColor : ColorValue := ( Red:=1.0, Green:=0.0, Blue:=0.0 ); END_VAR
  3. To call a method on the ColorValue class use:
    VAR StateColor : ColorValue; END_VAR
    
    ColourValue.SetRGB( 1.0, 0.0, 0.0 );
    

Standards Compliance

Table 48 - IEC 61131-3 Third Edition.