Problem with str2enum

Just found a strange behaviour in using the standard str2enum function. Consider the following code ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
static void TestOfEnumsWithFirstValueBlank(Args _args)
{
    ProjCategoryType selectedType;
    str              s;
    ;
 
    selectedType = ProjCategoryType::None;
    s = enum2str(selectedType); // get enum string
    print s; // shows nothing, since None has no label.
 
    selectedType = str2enum(selectedType,s); // convert back
 
    if(selectedType == ProjCategoryType::None)
        print "'None' selected";
    else
        print "undefined..."; // sadly this is printed..
 
    pause;
}

This example code prints undefined, NOT ‘None’ selected. This because str2enum does not handle an empty string in a correct way, even if one of the values in the choosen enum has the emptry string as it’s label. This means values to str2enum must always be validated first before each call..

Leave a Reply

Your email address will not be published. Required fields are marked *