PLC

Object-oriented programming

TwinCAT 3 supports object-oriented programming and provides the following functionalities and objects for this purpose:

Methods (Method object)
Properties (Property object)
Interfaces (Object Interface, Implementation of an interface)
Inheritance
Definition of function blocks as Extensions of other function blocks
Definition of structures as Extensions of other structures
Definition of interface as Extensions of other interfaces
Calling Methods - Virtual Function Call
 

General notes

If methods provide return values, these should be set in the method and evaluated at the point where the method is called.
If methods, functions or properties return structured return types (e.g. a structure or a function block), these return types should be declared as "REFERENCE TO <structured type>".
Returning structured data as a direct return type ("<structured type>") is inefficient because the data is copied multiple times.
If "REFERENCE TO <structured type>" is used instead, on the one hand this is more efficient because the data is not copied, and on the other hand a single element of the structured data type can be accessed directly when the method/function/property is called.
For more information and an example see the Object method description.
Programming of object-oriented implementations should be event-based.
Program elements should not be called cyclically without reason.
Usually, it makes sense to call a program element when a certain event occurs.
A method or property call via an unassigned interface variable has the same effect as a NullPointer call. Before you can use the interface variable, you must assign a corresponding function block instance to it (instance of a function block that implements the interface).
To ensure that the interface variable does not correspond to a NullPointer, the interface variable can be checked for unequal 0 before it is used (e.g. IF  (iSample <> 0) THEN iSample.METH (); END_IF).
 

See also:

Documentation samples: Sample: Object-oriented program for controlling a sorting plant
Additional information
Object Function block
Object Method
Object Property
Object Interface
Extending a function block
Extending a structure
Extending interfaces
Implementation of an interface
Calling Methods - Virtual Function Call