'Difficulty initialising an array of strings in structured text

Structured text seems to delight in making the simple really difficult (much more used to C). Could somebody please rearrange this so it will compile.

VAR_GLOBAL ErrorStg : Array[0 .. 10] of STRING[16] := "Good","Bad","Fsked"; END_VAR



Solution 1:[1]

Did you read the compiler error at all...?

The working version at least on CODESYS 3 based platforms:

ErrorStg : ARRAY[0 .. 10] OF STRING[16] := ['Good','Bad','Fsked']; 

The working version at least on CODESYS 2 based platforms:

ErrorStg : ARRAY[0 .. 10] OF STRING[16] := 'Good', 'Bad', 'Fsked'; 

You should use ' instead of " with regular strings.

Solution 2:[2]

When you initialize an array on the definition line, you have to give all of the values (all 11 in your case).

As an alternative, I would suggest a much easier solution - use an init routine to assign the values. If you are opposed to “burning boot time”, you can still solve by defining a FB called InitGlobals and then define a FB_INIT method and put your assignments there. FB_INIT executes as soon as the object exists and not when your program runs. Add an instance of InitGlobals to your code, of course.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Scott