THIS

THIS is a special variable 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 only use THIS in methods and function blocks. THIS is available for implementation in the input assistant in the Keywords category.

Dereferencing the pointer: THIS^

Using the THIS pointer:

Examples:

ST:

THIS^.METH_DoIt();

FBD/CFC/LD:

THIS 1:
THIS 2:

THIS is not implemented for Instruction List (IL).

Examples:

(1) The function block variable nVarB is shadowed by the local variable nVarB.

FUNCTION_BLOCK FB_A
VAR_INPUT
    nVarA: INT;
END_VAR

nVarA := 1;

FUNCTION_BLOCK FB_B EXTENDS FB_A
VAR_INPUT
    nVarB : INT := 0;
END_VAR

nVarA := 11;
nVarB := 2;

METHOD DoIt : BOOL
VAR_INPUT
END_VAR
VAR
    nVarB : INT;
END_VAR

nVarB := 22; // The local variable nVarB is set.
THIS^.nVarB := 222; // The function block variable nVarB is set even though nVarB is obscured.

PROGRAM MAIN
VAR
   fbMyfbB : FB_B;
END_VAR

fbMyfbB(nVarA:=0, nVarB:= 0);
fbMyfbB.DoIt();

(2) A function call needs the reference to the own FB instance.

FUNCTION F_FunA 
VAR_INPUT
    fbMyFbA : FB_A;
END_VAR
...;

FUNCTION_BLOCK FB_A
VAR_INPUT
    nVarA: INT;
END_VAR
...;

FUNCTION_BLOCK FB_B EXTENDS FB_A
VAR_INPUT
    nVarB: INT := 0;
END_VAR

nVarA := 11;
nVarB := 2;

METHOD DoIt : BOOL
VAR_INPUT
END_VAR
VAR
    nVarB: INT;
END_VAR

nVarB := 22;  //The local variable nVarB is set. 
F_FunA(fbMyFbA := THIS^);  //F_FunA is called via THIS^.

PROGRAM MAIN
VAR
    fbMyFbB: FB_B;
END_VAR

fbMyFbB(nVarA:=0 , nVarB:= 0);
fbMyFbB.DoIt();

See also: