4

The main goal of defining enumerators is to assign a variable to some numbers and their equal strings as I understand.

We can define var a as an enum everywhere in the initializing section of our Program or Function Block like this:

a:(start,stop,operate);

tough I don't know why we can't see that in tabular view but there there is a big question that:

What is the benefit of defining enumerators as a DUT?

asys
  • 667
  • 5
  • 20

4 Answers4

5

There are 3 main benefits for me:

  1. You can use the same enum in multiples function blocks
  2. You can use TO_STRING on enums declared as DUTs (After enabling it with {attribute 'to_string'} Infosys
  3. You can use refactoring on names of each component, which is impossible with local enums
Jacek Domański
  • 563
  • 3
  • 7
4

When defining an enum as a DUT it is available everywhere in your code (global scope). This is helpful in many cases, but in general it is not good programming practice to have a lot of stuff available in the global scope.

Here is a bit elaboration on the topic.

Mikkel
  • 138
  • 7
  • 1
    Just in addition to all the points that Jakob makes in that post: If you are exporting the FB (via library, or just copy paste) you no longer have to worry about dealing with 2 file objects. Any time there is a relation between two objects it is possible to have mismatched versions which can impact functionality. – Steve Nov 23 '21 at 18:06
1

In addition to the above, one benefit is that if you are using an enumeration for something like FB states, you will be able to see the descriptive status name when the program is running (READING, WRITING, WAITING, ERROR, etc.).

You can see it in the variable declarations section, in-line with your code, or in the watch window. You don’t have to remember what status number was defined in your state machine.

This benefit comes with local enumerations or DUT (global) enumerations.

1

In addition to other good points already made, there is another big advantage to enumerations : you can use the enumeration as the type of a variable, and when you do that, the compiler will (if {attribute 'strict'} is used in the enumeration declaration, which it probably should) refuse an assignment to that variable of a value that is not allowed by the enumeration.

In other words, you get rid of a whole class of failure modes where the variable ends up having an invalid value due to some coding mistake the compiler cannot catch.

It takes a trivial amount of time to create an enumeration, and it has benefits on many levels. I would say the real question is why not use them whenever a fixed list of meaningful values needs to be expressed.

Fred
  • 6,590
  • 9
  • 20