REFERENCE TO properties in TwinCAT 3 / CoDeSys
Key nuances of working with REFERENCE TO properties: `REF=` vs `:=` operators, getters, setters, and online monitoring of complex types.
A PROPERTY of type REFERENCE TO is a powerful feature that comes with several important nuances:
1️⃣ Getter and the REF= Operator
The getter must always use the REF= operator. Be careful and do not confuse it with :=, otherwise you will get an exception and PLC runtime will stop.
2️⃣ Setter and Reference Assignment
The setter allows to pass a new reference to the property. It also expected to use the REF= operator inside setter, but you can implement custom logic there. The key point is to understand that the incoming value is a reference and it can be null.
3️⃣ Value Assignment via :=
Assigning a value to the property using the := operator is completely independent of whether a setter is implemented or not.
4️⃣ Impossibility of Read-Only Protection
Unfortunately, it is currently impossible to prevent value assignment to such a property (i.e., make it truly read-only).
5️⃣ Monitoring Complex Data Types
Such properties allow you to inspect complex data types online when combined with the {attribute 'monitoring' := 'call'} pragma. Examples of complex data types include arrays, structures, and other classes.

#TwinCAT3 #CoDeSys #PLC #StructuredText #IndustrialAutomation #Beckhoff #OOP