Working with enumerations in scripts

This article shows how to work with enumerations in scripts.

The following script searches an enumeration value by its name and then passes the next value of the enumeration to the process context.

First, you have to add the following namespaces:

using EleWise.ELMA.Model.Metadata;
using EleWise.ELMA.ConfigurationModel;

Code of the script:

public virtual void SearchEnums (Context context)
{
var enumMetadata = (EnumMetadata)MetadataLoader.LoadMetadata(typeof(MyEnumeration)); // loading enumeration metadata. MyEnumeration is a class of our enumeration
var enumValue = enumMetadata.Values.FirstOrDefault(v => v.DisplayName == context.SearchString); // searching for the enumeration value that equals the search string

if (enumValue != null)
{
var value = (MyEnumeration)enumValue.EnumValue; //receiving the enumeration value and casting it to the type of our enumeration
context.NumberOfElements = value.GetHashCode(); // receive the position number of the enumeration item

if (value.GetHashCode() == 3) // our enumeration contains three items. If the selected item is third, then go back to the first item
{
context.NextEnumerationItem = MyEnumeration.Name1; // go back to the first item
}

else
{
context.NextEnumerationItem = value + 1; // get the value of the next enumeration item
}
}
}

MyEnumeration – the type of enumeration that you are working with;

context.SearchString – the variable of String type. You use it to search the enumeration value;

context.NumberOfElements – the variable of Integer type. It stores the number of enumeration items;

context.NextEnumerationItem – the variable of MyEnumeration type. It will contain the value of the next enumeration item.