Showing posts with label enum. Show all posts
Showing posts with label enum. Show all posts

Wednesday, June 30, 2010

How to translate an enumerated text

You can easily convert an enumerated text to a string value with the enum2str function. Example:

info(enum2str(SalesType::Sales));

But this will get you the label in the default language. What about other languages?
The following small job will get you the translated label info for all values of a specified enum, with a specified language.

static void Enum2Label(Args _args)
{   DictEnum                dictEnum;

int                     valueIndex;
LanguageId              languageId='de';

int                     enumId=enumNum(SalesType);
str                     labelId;
;

dictEnum = new DictEnum(enumId);
if (dictEnum)
{
for (valueIndex = 0 ; valueIndex < dictEnum.values(); valueIndex++)
{
labelId = dictEnum.index2LabelId(valueIndex);
info(SysLabel::labelId2String2(labelId, languageId));
}
}
}
Result: Journal Angebot Dauerauftrag Auftrag Zurückgegebener Auftrag Abrufauftrag Artikelbedarf

Similar code as above is used in the SRS classes in Ax. They make it possible for humans to understand and interpret the different enum values, as they merely are integer values in the database and as such for third party applications.

Wednesday, November 25, 2009

Enumerating with CLR Interop

Like seen in previous posts on this blog, with CLR Interop, a whole world of programming libraries are available to Ax.
But these come with little annoyances as well. Like when you try to use an enum in your programming library.
As an example, take a look at following code:

static void ClrEnum(Args _args)
{ System.Drawing.Image myImage;
System.Drawing.RotateFlipType myRotateFlipType;
;
myImage = System.Drawing.Image::FromFile(@'C:\MyBitmap.bmp');
myRotateFlipType=System.Drawing.RotateFlipType::Rotate90FlipNone();
myImage.RotateFlip(myRotateFlipType);
myImage.Save(@'C:\NewBitmap.bmp');
}

Everything looks OK. When typing in the code, IntelliSense even added the enumerations, but now if fails to compile. Saying

The class System.Drawing.RotateFlipType does not contain this function.

When working with CLR Interop, you might get this error from time to time. You check and doublecheck, there is no typing error.

So this wouldn't be much of a blog post if it didn't offer some workaround.

You can use the ClrInterop::parseClrEnum command.
With this command, you can convert a string value of an enumerated constant to an equivalent CLRObject instance.

Like this:

myRotateFlipType=ClrInterop::parseClrEnum('System.Drawing.RotateFlipType','Rotate90FlipNone');

Now that compiles! So now, enumerations in assemblies won't bother you again.