THIS
Pointer
For each function block, a pointer with name THIS
is automatically available. It points to its own function block instance.
This provides an effective solution for the following issues:
If a locally declared variable in the method hides a function block variable.
If you want to refer a pointer to its own function block instance for using in a function.
THIS
may only be used in methods/properties/actions/transitions and in the associated function block implementation.
THIS
must be written in capital letters. Other spellings are not accepted.
Because THIS
is a pointer to the function block, you have to dereference it to access an element of the function block: THIS^.METHDoIt
.
THIS
Call in Different Implementation Languages
Implementation Language |
Example |
---|---|
ST |
|
FBD/CFC/LD |
![]() |
THIS
is not yet implemented for Instruction List.
Local variable iVarB
shadows 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; // Here the local iVarB is set.
THIS^.iVarB := 222; // Here the function block variable iVarB is set, although iVarB is overloaded.
PROGRAM PLC_PRG
VAR
MyfbB: fbB;
END_VAR
MyfbB(iVarA:=0 , iVarB:= 0);
MyfbB.DoIt();
Function call that needs a 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; //Here the local iVarB is set.
funA(pFB := THIS^); //Here funA is called with THIS^.
PROGRAM PLC_PRG
VAR
MyfbB: fbB;
END_VAR
MyfbB(iVarA:=0 , iVarB:= 0);
MyfbB.DoIt();