Friday, 17 April 2015

Assigning enum value to variable from enum name

Working with utilities there are alot of file exchanges - alot.

Today  we got a strange case though. In a file we receive a value that represents the name of a enum value, e.g. E17. The label of the enum value is Consumption, so we wanted to assign the value to a field value in a given table, so this is what we came up with

    // must assign Consupmtion and not E17
    JournalConnection     journalConnection;
    str                   typeOfMeteringPoint = "E17";
   
    SysDictEnum             dictEnum    = new SysDictEnum(enumNum(ConnectionTypes));
    ;
   

    journalConnection.type = dictEnum.symbol2Value(typeOfMeteringPoint);






we also came up with this one where we inserted the option of a small if-statement to check if the name of the enum value is found on the enum:
    
    JournalConnection           journalConnection;
    ConnectionTypes             connectionTypes;
    str                         typeOfMeteringPoint = "E17";
   
    SysDictEnum             dictEnum    = new SysDictEnum(enumNum(ConnectionTypes));
    Counter                 values      = dictEnum.values();
    int                     idx;
    ;
   
    for(idx = 0; idx< values ;idx++)
    {   
        if(typeOfMeteringPoint == dictEnum.index2Symbol(idx))
        {
            journalConnection.type = str2enum(ConnectionTypes,dictEnum.index2Name(idx));
        }

    }