THIS

THIS is a special variable and 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 obscures 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:

../_images/4dcd26b2e9c4c99ac0a8640e000e51f7

Note

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

Examples

  1. The local variable iVarB obscures the function block variable iVarB.
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();
  1. 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();

See also