Interface pointer / INTERFACE

An interface pointer (also called an interface variable) stores the address of a function table or a function block instance that implements the interface. All methods and properties that are defined by the interface can be called via this indirection with an interface pointer. These methods and properties must therefore be implemented by the function block whose instance is assigned to the interface pointer.

An instance of a function block that implements a corresponding interface must always be assigned to an interface pointer before use.

It is recommended to check the interface pointer for validity (<> 0) before use.

Syntax:

<Kennzeichner> : <Interfacetyp>;
FUNCTION_BLOCK <Funktionsbausteintyp> IMPLEMENTS <Interfacetyp>

Example:

FUNCTION_BLOCK FB_Sample IMPLEMENTS I_Sample
VAR
    ipSample : I_Sample;
    fbSample : FB_Sample;
    nResult  : INT;
END_VAR
ipSample := fbSample;
 
// calling a method of this interface
IF ipSample <> 0 THEN
    nResult := ipSample.Add(3, 6); // result is 9
END_IF

The use of interfaces and interface pointers is explained in detail in the section Object-oriented programming > Object Interface.

Type conversions are possible with the operator __QUERYINTERFACE().