Working with Enumerated Types (ENUMs)
If you want to categorize objects as different types, instead of using a number or a free format string, use an Enumerated Type (ENUM). Enumerated Types provide a better solution because they are restricted, in that they can only take a fixed limited number of values. The names of the values can be made meaningful.
The qualified name of enumerations to be used in script is similar to the Factory names, with the qualified name formatted to replace dot '.' by '_' an underscore character. For example, com.example.shared.ColorEnum will be used as com_example_shared_ColorEnum in script.
Unqualified names (for example, Color.GREEN) are supported only in validation for unambiguous situations (the unqualified name will not be available in content assist).
An ENUM is created in the BOM editor by selecting the Enumeration type from the Elements section of the Palette. Having selected the Enumeration Element, it can be named, and values can be added to it. The following is an example of an Enumerated type called SpaceType, with PLANET, MOON, ASTEROID, and STAR values:
Having defined the Enumeration type, a class attribute can be set to that type:
The following is an example of a loop, which can be used to calculate the average weight of the planets in a list of astronomical bodies.
var dTotalKgs = 0.0; var dPlanetCount = 0; for (var iterator = solarSystem.objectList.listIterator(); iterator.hasNext(); ) { var body = iterator.next(); if (SpaceType.PLANET == body.type) { dTotalKgs = dTotalKgs + body.weightKgs; dPlanetCount ++; } } solarSystem.averagePlanetWeight = dTotalKgs / dPlanetCount; |
A Business Object attribute that is configured to be of a particular Enumeration type can only be assigned with values of that enumeration type. Either constants of that type, such as:
or other attributes of that type, such as:
An attribute of an enumeration type cannot be assigned from any other type. For example, the following is not valid:
The Enumerated Type also enables you to get a specific enumeration literal from its text value. For example, if you want to pass an enumeration value as a string to an external application and then pass it back to a process. You can use this in scripts with all available enumeration data types. Either text data types, as in the following example:
and non-text data types, such as: