Parameterized Property syntax for array element processing

e13740e
2025-10-09
2025-10-19
  • e13740e - 2025-10-09

    Hello.

    I'm trying to create a Parameterized Property(CDS V3.5 SP21) to avoid copy-pasting code for processing array elements. I can't seem to get any result. Perhaps I am making a mistake with the syntax. None of the declaration options below are accepted by the compiler.

    If you know and use them (Parameterized Properties) in your work, please let me know.

    Thank you in advance.


    Option 1:

    PROPERTY <property_name> : <data_type>
    VAR_INPUT
    <index_name> : <index_type>; // One or more index parameters are declared here
    END_VAR</index_type></index_name></data_type></property_name>

    GET
    Code that uses <index_name> to return a value
    END_GET</index_name>

    SET
    Code that uses <index_name> and the implicit variable <property_name> to write a value
    END_SET</property_name></index_name>

    END_PROPERTY


    Option 2:

    PROPERTY <property_name> (<index_name> : <index_type>) : <data_type></data_type></index_type></index_name></property_name>

    GET
    Code that uses <index_name> to return a value
    END_GET</index_name>

    SET
    Code that uses <index_name> and the implicit variable <property_name> to write a value
    END_SET</property_name></index_name>

    END_PROPERTY

     

    Last edit: e13740e 2025-10-09
  • e13740e - 2025-10-19

    Since parameterized (indexed) properties are not possible, I decided to use architectural approach "Indexing Property Adapter" to achieve the intended goal β€” avoiding code duplication for validated access to array elements.

    Essence:
    Create a Function Block Adapter (FB_PropertyAdapter) that encapsulates the logic for working with a single element of the target array (e.g., with one settings structure).
    Declare Properties inside the adapter block for accessing each individual field of the data structure. All validation logic is implemented within the Set accessors of these properties.
    Data Binding via VAR_IN_OUT: The adapter block receives a reference to a specific data element from the global array through its VAR_IN_OUT section, which ensures direct work with the original data without copying.
    Create an Array of Adapters: In the parent POU (e.g., FB_SettingsManager), an array of these adapter blocks is created β€” one for each element of the global array that needs to be managed.
    Initialize Bindings: In a FOR loop, each adapter instance from the array is given a reference to the corresponding data element.

    Result:
    This approach allows accessing the data via the index of the adapter array, and then through the property name, which simulates the behavior of an indexed property:
    MyAdapterArray[Index].MyProperty
    Thus, the validation and data access logic is written only once inside the adapter block and is then reused multiple times by creating instances of it in an array. This completely solves the problem of code duplication while providing a clean, scalable, and object-oriented architecture compatible with standard CODESYS features.

    Question closed.

     

    Last edit: e13740e 2025-10-19

Log in to post a comment.