Home > Back-end >  Difficulty initialising an array of strings in structured text
Difficulty initialising an array of strings in structured text

Time:04-01

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

CodePudding user response:

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']; 

You should use ' instead of " with regular strings.

CodePudding user response:

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.

  • Related