Overview of the THIS Keyword in Siemens SCL
The THIS keyword is a contextual element reference that appears only inside Array Data Blocks (ArrayDB) authored in SCL (Structured Control Language) on the S7-1500 CPU family in TIA Portal. It is not a general-purpose identifier and it is not part of the SCL language reserved-word set on S7-300/S7-400 or on S7-1200. On those older or smaller platforms it is either silently rejected by the compiler or it does not appear in the editor's autocomplete at all.
When an SCL source block is generated as an ArrayDB of a complex data type (UDT, FB, or structured PLC data type), the editor and compiler allow the programmer to address the current array element using the literal token THIS. The keyword resolves at runtime to the index position implied by the surrounding access path, and is most commonly observed in:
- Arrays of FB instances (multi-instance arrays) used for batch / parallel control logic.
- Arrays of PLC data types used as lookup tables where the index is computed inside a
FORloop. - Generated SCL sources where the programmer needs a self-reference placeholder during inline edit / autocomplete.
Because THIS only resolves in a specific block type and only on S7-1500 firmware, engineers copying snippets from older S7-300/S7-400 projects, or from PC programming languages, will get either compile errors or unexpected behavior. This reference documents the precise syntax, the platforms on which it is legal, and the verification steps to confirm correct use.
THIS. Documentation is split between the Programming and Operating Manual S7-1500 and the context-sensitive help on ArrayDB declarations. Treat third-party blog statements about THIS with caution; the only authoritative references are Siemens manuals.Prerequisites: TIA Portal, Firmware, and Block Types
Before using the THIS keyword, confirm the engineering environment meets the following requirements:
| Item | Required | Notes |
|---|---|---|
| Engineering tool | TIA Portal V13 SP1 or later (V16+ recommended) | V13 SP1 introduced initial ArrayDB grammar; V14 added structured PLC data types; V15 extended method support. THIS token visibility varies. |
| CPU family | S7-1500 (including ET 200SP CPU, S7-1500 Software Controller) | NOT supported on S7-300, S7-400, S7-1200 G1/G2, WinAC, or older |
| CPU firmware | V1.7 or higher for full ArrayDB; V2.0+ recommended | Early V1.5/V1.6 firmware accepts ArrayDB but rejects THIS at compile time |
| Block type | ArrayDB (data block generated from a PLC data type or FB) | Stand-alone DBs without an array declaration do not accept THIS
|
| Language | SCL | LAD/FBD do not expose THIS; STL has equivalent via AR1/DI but no THIS token |
The official SCL programming reference is the SIMATIC S7-1500 Programming Guideline and the in-product help delivered with TIA Portal. Use F1 inside an ArrayDB SCL source for the context-sensitive grammar page. For block-creation conventions, see the Siemens entry Using regions - STEP 7 (TIA Portal V21 docs).
Syntax of the THIS Token
In its canonical form the keyword is written as the bare token THIS and is used as an element-designation placeholder for the current array element inside an ArrayDB. The token is case-insensitive in TIA Portal's editor (THIS, This, and this are accepted and rewritten to upper case on compile).
// Canonical: index access with THIS as element designator
"MyArrayDB".THIS[i].TagName := 1;
// Equivalent absolute index form (resolved at compile time)
"MyArrayDB".Element[i].TagName := 1;
// Read access
wValue := "MyArrayDB".THIS[i].TagName;
The following grammar rules apply:
-
THISmust appear after the ArrayDB identifier and before the indexed element member. - An integer or
DINT/INTindex expression must follow in square brackets. - The index must be in range of the declared array bounds; otherwise the compiler flags Range error during compilation or Index out of bounds at runtime.
-
THISis only legal on the left side of an indexed member access; it cannot stand alone as a value. -
THIScannot be assigned to. The constructTHIS := ...;is rejected with the error L-value required.
THIS in the ArrayDB Context
An ArrayDB is a data block whose PLC data type is declared as an ARRAY of either:
- A PLC data type (UDT) - structured template referenced by multiple ArrayDBs.
- An FB - the ArrayDB then acts as a multi-instance array, retaining the FB's instance-DB interface.
Both configurations place the keyword THIS in scope. The conceptual model is that THIS means "the element at the currently implied index". The compiler infers the index from the enclosing expression. In a FOR loop, the index comes from the loop variable; in a direct assignment, the index is the one written explicitly.
// PLC data type "RecipeUDT"
TYPE "RecipeUDT"
STRUCT
RecipeID : DINT;
SetPoint : REAL;
Tolerance : REAL;
bActive : BOOL;
END_STRUCT;
END_TYPE
// ArrayDB "RecipeDB" declared as ARRAY[0..99] OF "RecipeUDT"
DATA_BLOCK "RecipeDB"
{ S7_Optimized := 'TRUE' }
VERSION : 0.1
STRUCT
Recipes : ARRAY[0..99] OF "RecipeUDT"; // implicit / default array slot
END_STRUCT
END_DATA_BLOCK
Inside the SCL body of a separate code block (FC/FB) that operates on this DB, the engineer can write:
FOR i := 0 TO 99 DO
// THIS refers to the current RecipeUDT element of the ArrayDB
"RecipeDB".THIS[i].SetPoint :=
"RecipeDB".THIS[i].SetPoint * 1.05;
END_FOR;
The compiled MC7/SCL output is identical to writing "RecipeDB".Recipes[i].SetPoint directly. The keyword is a readability aid for engineers who reason about ArrayDBs as a sequence of this objects.
Array of FB Instances (Multi-Instance Array)
When the ArrayDB is built from an FB, THIS also exposes the FB's input, output, in-out, static, and temp regions. The FB retains its multi-instance capability: every array element has its own instance-DB storage but shares the FB code.
FUNCTION_BLOCK "ValveCtrl"
VAR
iOpen : BOOL;
rPos : REAL;
iIndex : INT;
END_VAR
BEGIN
// FB body code; this single copy is shared by every array element
IF iOpen THEN
rPos := 100.0;
END_IF;
END_FUNCTION_BLOCK
DATA_BLOCK "ValveBankDB"
VERSION : 0.1
STRUCT
Valves : ARRAY[0..15] OF "ValveCtrl";
END_STRUCT
END_DATA_BLOCK
Cyclical access from a separate FB uses THIS:
FOR i := 0 TO 15 DO
// Accessing the FB instance via THIS
IF "ValveBankDB".THIS[i].iOpen THEN
"ValveBankDB".THIS[i].rPos := 100.0;
END_IF;
END_FOR;
Here, THIS resolves to the i-th FB instance. The behavior is functionally identical to:
"ValveBankDB".Valves[i].iOpen
"ValveBankDB".Valves[i].rPos
Differences From Object-Oriented THIS
Engineers familiar with C++, C#, or Java often expect this to refer to the current object instance. The semantics in Siemens SCL are narrower:
| Language | Refers to | Scope | Can be assigned |
|---|---|---|---|
C++/C#/Java this
|
Current object instance | Inside any non-static class method | No (read-only reference) |
Python self
|
Current object instance (explicit) | Inside instance methods | No (convention only) |
Siemens SCL THIS
|
Current array element of an ArrayDB | Only inside SCL code that accesses an ArrayDB element | No (compile error) |
Key distinctions to internalize before porting OO code patterns into SCL:
- SCL FBs are not classes. There is no inheritance, no virtual dispatch, no method overriding.
THISdoes not enable OO patterns. - The SCL FB's instance DB is selected by the call-site multi-instance mechanism, not by
THIS. Use the FB's input/output parameters to identify which array element is being processed. - You cannot use
THISto disambiguate a tag name from a parameter name (noTHIS.valueshadowing ofvalueparameter; SCL resolves naming through the block interface and the static/in-out sections). - The keyword is not required for SCL compilation. The compiler always accepts the explicit absolute path;
THISis purely a syntactic alias.
FOR-Loop Iteration Patterns
The recommended pattern for processing every element of an ArrayDB is a FOR loop with the loop variable used as the index. The index type must match the array bounds type; for an ARRAY[0..99] declared as INT range, declare i as INT or DINT.
FUNCTION_BLOCK "RecipeProcessor"
VAR
i : INT;
rSum : REAL := 0.0;
rAvg : REAL;
END_VAR
BEGIN
rSum := 0.0;
FOR i := 0 TO 99 DO
// Skip inactive slots
IF NOT "RecipeDB".THIS[i].bActive THEN
CONTINUE;
END_IF;
rSum := rSum + "RecipeDB".THIS[i].SetPoint;
END_FOR;
rAvg := rSum / 100.0;
END_FUNCTION_BLOCK
Alternative idiomatic forms that do not use THIS:
// Variant A: explicit absolute path
"RecipeDB".Recipes[i].SetPoint
// Variant B: aliased DB variable
#rSp := "RecipeDB".Recipes[i].SetPoint;
All three variants compile to the same MC7 code; pick whichever improves readability for the project standard. Avoid mixing THIS with the explicit .Recipes qualifier inside the same loop - the editor accepts both but team style guides typically require consistency.
Common Pitfalls and Editor Quirks
Several behaviors reported in practice are not bugs - they are context-sensitive editor features. Recognize each before assuming a defect.
| Symptom | Cause | Fix |
|---|---|---|
THIS appears in the autocomplete dropdown while typing THEN
|
Context-sensitive inline help; TIA Portal suggests THIS because the surrounding token prefix matches |
Disregard the suggestion or press ESC; complete the IF statement with the literal THEN token |
| Compiler error: Identifier 'THIS' not declared | The block is a regular DB, not an ArrayDB | Convert to ArrayDB by changing the block's PLC data type to an ARRAY of a UDT/FB |
| Compiler error: THIS not supported on S7-1200 / S7-300 | Cross-platform download attempted | Use an S7-1500 CPU; rewrite as explicit absolute indexing on smaller platforms |
| Runtime error: Area length error when reading | Index variable out of array bounds at runtime | Bound-check the index before the access; use VAL_OK from SCL_RNG or a manual IF i >= LOWER_BOUND AND i <= UPPER_BOUND
|
The keyword THIS survives even after renaming the DB |
Search/replace did not touch the array-element path | Use "Go to definition" (Ctrl+Shift+F12) to find all references and re-sync |
Verification and Compilation Checks
Use the following procedure to confirm a block that uses THIS compiles and behaves correctly:
- Compile the project. From the project tree right-click the S7-1500 station and choose Compile > Software (rebuild all). Watch the Info pane for Warning: identifier 'THIS' used but not declared - that indicates the block is not an ArrayDB.
-
Open the block properties. Confirm the DB's Type is Array DB and the assigned PLC data type is an
ARRAY. The properties page will show the element count and bounds. -
Cross-reference audit. From the SCL editor use Tools > Cross-references or Ctrl+Shift+F12 to list every
THISreference. Each occurrence must resolve to the same DB; mixed references signal a partial rename. -
Download and watch tables. Use a VAT (Variable Table) to monitor one array element while the
FORloop runs. Trigger an Execute once with a breakpoint on the array access; verify the index and the stored value. - Online / diagnostic buffer check. Confirm no Area length error or Index out of range events in the diagnostic buffer (CPU > Online & diagnostics > Diagnostic buffer).
-
Step through with breakpoints. Place a breakpoint on the line containing
THIS[i]. Cycle the PLC into Single-step; inspect the watch window for the resolved element address.
THIS token to be evaluated correctly by the editor's cross-reference engine.Edge Cases and Field-Proven Caveats
Several conditions produce behavior that surprises engineers who encounter THIS for the first time:
-
Nested arrays: If the ArrayDB contains an array of arrays (an
ARRAY[0..9] OF ARRAY[0..9] OF RecipeUDT), you must supply two indices:"MyDB".THIS[i][j].SetPoint. The keyword does not collapse multiple dimensions. -
AT view overlay: When using an
AToverlay on top of the ArrayDB, address the overlay explicitly.THISaddresses only the original symbolic member; mixing the overlay path withTHISproduces inconsistent results in the editor's tooltip. - POU in library form: Reusable FBs in a global library may reference an ArrayDB. Compile the library first; otherwise the consuming project shows Unknown identifier 'THIS' even though the source is correct.
-
Watch table naming: When adding an element from an ArrayDB to a watch table, prefer the explicit absolute path. Watch tables do not preserve
THIS; the table re-translates the token at insertion time. -
Online snapshot of optimized data: The online snapshot of an optimized block uses slot indices, not symbolic names.
THIS[i]resolves correctly in the online view, but the snapshot column header shows the absolute path with the resolved index.
Cross-Platform Behavior Matrix
| Controller family | Firmware | ArrayDB support |
THIS keyword |
|---|---|---|---|
| S7-1500 (incl. ET 200SP CPU, CPU 1518 MFP) | V2.0+ | Full | Supported |
| S7-1500 | V1.5-V1.9 | Limited (no FB-as-array) | Rejected at compile |
| S7-1200 G2 | V4.5+ | ArrayDB of UDT only | Not surfaced in editor |
| S7-1200 G1 | V4.x | DB of arrays only | Not supported |
| S7-300 / S7-400 | Any | DB of arrays only | Not supported |
| WinAC RTX / Software Controller | V15+ | ArrayDB supported | Supported on PC-based runtime |
Style-Guide Recommendation
Because THIS is legal but optional, organizations writing SCL style guides should adopt one of two rules and enforce it via code review:
- Use THIS exclusively: improves readability for engineers who think of the ArrayDB as a sequence of self-referenced objects. Easier to teach to OO-trained programmers.
- Never use THIS: forces every access through the explicit absolute path; maximum grep-ability and minimal IDE dependency.
Mixed usage across a project creates real maintenance friction because search-and-replace and refactoring tools do not always follow the keyword token through index expressions.
FAQ
What is the THIS keyword in Siemens SCL?
The THIS keyword in Siemens SCL is an element-designation token that resolves to the current array element of an Array Data Block (ArrayDB). It is legal only in SCL code that accesses an ArrayDB element on an S7-1500 CPU running firmware V2.0 or higher, and is compiled to the same MC7 output as the equivalent explicit index expression.
Is THIS supported on S7-1200 or S7-300 CPUs?
No. THIS is only surfaced and accepted by the SCL compiler on S7-1500 CPUs (including ET 200SP CPU and the S7-1500 Software Controller). On S7-1200 and S7-300/400 the compiler either rejects the token or does not list it in the autocomplete; use the explicit absolute index path instead.
Why does THIS appear in the TIA Portal autocomplete when I type THEN?
The TIA Portal editor is context-sensitive and matches on the leading characters of a token. While typing THEN the editor may surface THIS as a candidate because the prefix matches. Press ESC or continue typing past the N and the THEN keyword is selected instead. The suggestion is not an error.
Can I assign a value to THIS directly?
No. The construct THIS := ...; is rejected with an L-value required error. THIS must always be followed by an index and a member access, e.g. "MyDB".THIS[i].TagName := 1;.
Does THIS behave like 'this' in C++ or C#?
Only superficially. In C++/C# this refers to the current object instance inside a non-static method; in Siemens SCL THIS refers to the current array element of an ArrayDB and is not used for OO dispatch or parameter-shadowing. SCL FBs are not classes, so THIS does not enable inheritance or method overriding.