Search code examples
axaptadynamics-ax-2012x++

Display enum value based on field value


I want to return a enum value based on a field value.

For example:

If Free value field has the value '15', I want to return the enum value "1", because the Label of enum value 1 is: '10-20', because 15 is in the range of 10-20 I want to return that value.

I want to create a display method for this.

I guess I can accomplish this with a switch case scenario.
How can I best set this up?


Solution

  • Of course you can do it with a switch / case but if you deal with a range of values and a limited set of result values (your enumeration elements) then a simple if / else if is probably better suited

    So instead of stating each possible value (1, 2, 3, 4 bla bla) in your case branches do something like this

    int x;
    ;
    
    x = yourTable.YourField;
    if (x >= 1 && x <= 15)
    {
        return YourEnum::1to15;
    }
    else if (x >= 16 && x <= 20)
    {
        return YourEnum::16to20;
    }
    // other possible ranges
    else
    {
        throw YourEnum::Unknown;
    }