Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Single Line Array Write

2020-07-24
2020-07-24
  • jeremyshubert - 2020-07-24

    I can easily do the following:
    VAR
    arrIndicatorColor : ARRAY[1..5] OF STRING;
    END_VAR


    arrIndicatorColor[1] := 'Red';
    arrIndicatorColor[2] := 'Yellow';
    .
    .
    .
    arrIndicatorColor[5] := 'White';


    I'd prefer to set all the values of the array with a single line.
    ex:
    arrIndicatorColor := ['Red','Yellow','Green','Blue','White'];

    However this line of ST gives me an error "Unexpected array initialization". Is there a proper syntax? I can copy and paste, in my example it's only 5 array elements but if this was 10 elements with 10 cases, it becomes a lot of lines of ST.


    EDIT
    I have no problem with STRING vs WSTRING...just a typo since I'm not in the CODESYS IDE, changed to reflect. I don't want to initialize the array with certain values. I want to set all elements of the array using one line of ST.

     
    πŸ‘
    2

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 5


    Last edit: jeremyshubert 2020-07-25
  • Morberis

    Morberis - 2020-07-24

    Here is how that should look for initializing it with those strings

    arrIndicatorColor: ARRAY[1..5] OF STRING := ['Red', 'Yellow', 'Green', 'Blue', 'White'];

    The difference between yours and mine is you used " which indicates a wString and ' which indicates a String.

    Sorry at first I specified that address 1 was Red and address 2 was Yellow but that didn't work past Red. In case you're wondering if you want to initialize only part of the array it would look like this.

    arrIndicatorColor: ARRAY[1..5] OF STRING := ['Red', 'Yellow', 'Green', 2('')

    You can also use the input assistant and within that the array wizard to have it spit out an array variable with specific formating if you aren't sure how it should look.

     

    Last edit: Morberis 2020-07-24

Log in to post a comment.