Pointer: ‎THIS‎ Permalink

The ‎THIS‎ pointer is a special variable which is used for object-oriented programming.

‎THIS‎ is the pointer of a function block to its own function block instance. A ‎THIS‎ pointer is automatically available for each function block. You can use ‎THIS‎ only in methods and in function blocks. ‎THIS‎ is available for the implementation in the Input Assistant in the category “Keywords”.

Dereferencing of the pointer: ‎THIS^‎

Use of the ‎THIS‎ pointer

  • If a local variable overrides a function block variable in a method, you can set the function block variable with the ‎THIS‎ pointer. See example below (1)

  • If the pointer to the function block's own function block instance is referenced for use in a function. (See example below (2))

Examples

ST

‎THIS^.METH_DoIt();‎

FBD/CFC/LD

_cds_img_pointer_this.png

Example

The ‎iVarB‎ local variable overrides the ‎iVarB‎ function block variable.

FUNCTION_BLOCK  fbA
VAR_INPUT
    iVarA: INT;
END_VAR
iVarA := 1;

FUNCTION_BLOCK fbB EXTENDS fbA
VAR_INPUT
    iVarB: INT := 0;
END_VAR
iVarA := 11;
iVarB := 2;

    METHOD DoIt : BOOL
    VAR_INPUT
    END_VAR
    VAR
        iVarB: INT;
    END_VAR
    iVarB := 22;    // The local variable iVarB is set.
    THIS^.iVarB := 222;    // The function block variable iVarB is set even though iVarB is obscured.

PROGRAM PLC_PRG
VAR
   MyfbB: fbB;
END_VAR

MyfbB(iVarA:=0, iVarB:= 0);
MyfbB.DoIt();

Example

A function call requires the reference to its own instance.

FUNCTION funA
VAR_INPUT
    pFB: fbA;
END_VAR
...;

FUNCTION_BLOCK  fbA
VAR_INPUT
    iVarA: INT;
END_VAR
...;

FUNCTION_BLOCK fbB EXTENDS fbA
VAR_INPUT
    iVarB: INT := 0;
END_VAR
iVarA := 11;
iVarB := 2;

    METHOD DoIt : BOOL
    VAR_INPUT
    END_VAR
    VAR
        iVarB: INT;
    END_VAR
    iVarB := 22;    //The local variable iVarB is set.
    funA(pFB := THIS^);    //funA is called via THIS^.

PROGRAM PLC_PRG
VAR
    MyfbB: fbB;
END_VAR
MyfbB(iVarA:=0 , iVarB:= 0);
MyfbB.DoIt();

‎THIS‎ is not yet implemented for the instruction list (IL).