1

I'm using Connected Components Workbench (CCW) and the syntax for a case statement that is given in the documentation is:

CASE <integer_expression> OF
    <value> : <statement1> ;
              <statement2> ;
              <statementsN>

<value> : <statements> ;

<value>, <value> : <statements>;
...
ELSE
<statements> ;
END_CASE;

I would like to avoid using explicit numbers (literals):

CASE STATE OF
STATE_A:
    // Some code
STATE_B:
    // Some code
ELSE
END_CASE;

When I use variables for the case labels I get a compilation error:

Error 1 STATE_A:unexpected statement

Is there a way to avoid explicit numbers for the different cases?

Sparkler
  • 2,581
  • 1
  • 22
  • 41

1 Answers1

3

One option is to used enumerations instead. You first need to define an enumeration as a type. This enumeration can in turn be whatever primitive data type you want. If you add the pragma "qualified_only", you can make the code look much elegant. Say for instance you define a new type as:

{attribute 'qualified_only'}
TYPE E_State :
(
    A := 0,
    B := 1,
    C := 2
) USINT;
END_TYPE

What you're basically saying here is that this is an enumeration that will take up 1 byte of space (as the base-type is USINT), and that if you want to use the enumeration it needs to be preceeded with the name of the enumeration (in this case "E_State"). Note that you don't need to explicitly declare the numbers here. If you don't write any numbers, the compiler will automatically assume the first one is zero and add one to each one following. So this will work as well:

{attribute 'qualified_only'}
TYPE E_State :
(
    A,
    B,
    C
) USINT;
END_TYPE

You don't even need to declare the base-type. If you don't declare anything (so not writing USINT above), the compiler will automatically assume it's an INT.

Using this in a switch-case in a program or function block would make it look like this:

PROGRAM MAIN
VAR
    eState : E_State;
END_VAR

Body:

CASE eState OF
    E_State.A : 
        // Do something A
    E_State.B : 
        // Do something B
    E_State.C : 
        // Do something C
    ELSE
        // Do something
END_CASE
Jakob
  • 1,288
  • 7
  • 13
  • You can also remove the `{attribute 'qualified_only'}`from enumeration definition. By doing this the `E_State` prefix can be forgotten and you can use values `A`, `B`, `C`.. instead of `E_State.A`. – Quirzo May 02 '18 at 05:37