str2Enum Function

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

Retrieves the enum element whose localized Label property value matches the input string.

enum str2Enum(enum _type, str _text)

Parameters

Parameter

Description

_type

A variable declared of the enum type.

_text

The localized Label property text of the target element in the enum.

Return Value

An element of the target enum, which also represents an int.

Remarks

The related function enum2str returns the value of a Label property from one element on the enum. The value returned by enum2str function can be the input for the _type parameter of the str2enum function.

An appropriate value for the _text parameter is enum2Str(BankAccountType::SavingsAccount).

Each element of an enum has a Name property and a Label property. In a fresh install of Microsoft Dynamics AX, the Name values are almost always English words. In the English edition of Microsoft Dynamics AX, the Label property value is almost always identical to the Name value. But in the non-English editions the Label values are localized, and therefore do not match the Name values.

Example

To avoid string mismatches due to localization to other spoken languages, we recommend that you use the enum2str function to generate the input into the str2enum function.

The following example shows the appropriate way to use the str2enum function with the enum2str function.

    static void str2Enum_AcrossLangs(Args _arg)
    {
        BankAccountType bat;
        str sEnumValueLabelLocalized;
        int nInt;
    
        // enum2str.
        sEnumValueLabelLocalized = enum2str
            (BankAccountType::SavingsAccount);
        info("Localized friendly string: "
            + sEnumValueLabelLocalized);
    
        // str2enum.
        bat = str2Enum(bat, sEnumValueLabelLocalized);
        nInt = bat;
        info("nInt = " + int2str(nInt));
    
    /********** Actual output: 
    Message (04:32:12 pm)
    Localized friendly string: Savings account
    nInt = 1
    **********/
    }

See also

enum2Str Function

any2Enum Function

Announcements: To see known issues and recent fixes, use Issue search in Microsoft Dynamics Lifecycle Services (LCS).