Pages

Monday, January 7, 2008

Get the literal label of an enum value

I couldn't find a function in the standard to give me the literal label (@SYSxxxx) of an enum value, so I came up with this prototype:

static void literalLabelsFromEnum(Args _args)
{  
    #Properties         
    SysDictEnum sysDictEnum = new SysDictEnum(enumNum(SysDimension));  
    TreeNode    treeNode;  
    int         x;     
    ;
     
    for (x = 0; x <= SysDictEnum.lastValue(); x++) 
    {           
        treeNode = TreeNode::findNode(sysDictEnum.path() + '\\' + sysDictEnum.index2Symbol(x));           
        print sysDictEnum.index2Label(x)+ '  ' + findProperty(treeNode.AOTgetProperties(),#PropertyLabel);     
    }     
    pause;
}

9 comments:

Yury said...

I guess the task can be solved with enum2str function.
In general, it's better to avoid metaprogramming in AX.

Palle Agermark said...

Hi Yury,

enum2Str returns the value of the enumeration, i.e. "Yes" and not the label id which is what I want in this case.

http://msdn.microsoft.com/en-us/library/aa845422(v=AX.50).aspx

enum2LiteralStr in AX 2012 may be useful, but I can't figure out how to use it. No matter what I try I get a compilation error.

http://msdn.microsoft.com/en-us/library/gg876593.aspx

Cheers,
Palle

naski said...

this really is useful. btw there's some error in for statement : )

thanks for sharing ^^

naski said...

this is really helpful
thanks for sharing : )

naski said...

i found something easier

static void Datatypes_enum2str(Args _args)
{
SalesType salesType; // a standard enum in Ax
;
salesType = SalesType::Sales;
info(strfmt("The name of the current sales-type is '%1'",enum2str(salesType)));
}

enum2str . such function exists =="

Palle Agermark said...

I think my post maybe isn't clear enough. I do not want the text represented by the label. I want the literal label, i.e. "@SYS123".

I have modified this last sample, to show the difference.

static void Datatypes_enum2str(Args _args)
{
#Properties
SysDictEnum sysDictEnum = new SysDictEnum(enumNum(SalesType));
TreeNode treeNode;
SalesType salesType; // a standard enum in Ax
;
salesType = SalesType::Sales;
info(strfmt("The name of the current sales-type is '%1'",enum2str(salesType)));

treeNode = TreeNode::findNode(sysDictEnum.path() + '\\' + sysDictEnum.value2Symbol(salesType));
info (strfmt("The label of the current sales-type is '%1'", findProperty(treeNode.AOTgetProperties(),#PropertyLabel)));
}

Pradeep said...

This is another way to do it
SysDictEnum dictEnum = new SysDictEnum(enumNum(SalesType));
Counter values = dictEnum.values();
int idx;

for(idx = 0; idx< values ;idx++)
{
print dictEnum.index2Label(idx);
pause;
}

Baber said...

Use enum2Value function, if you want to get the label of enumeration element.

Baber said...

Use enum2value function if you want to get the enumeration element label instead of the element itself.