web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to set Enum value with out using Enumtype

(0) ShareShare
ReportReport
Posted on by

Hi All!!!!,

Please Help me!!!!!

I am having a problem to set enum value with out using the enum type.

For example, normally we set enum field's value as - Table.fieldName = str2enum(EnumType, "string"); (or) Table.fieldName = EnumType::Element;

But I need to set the enum value with out using that EnumType, Whether it is possible!!!!

Thanks in Advance!!!

Regards,

Deepak M

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    So what information do you have about the value? A string representation?

    And where do you try to assign it to? In case of a field, you can get EnumType from field's properties, can't you?

  • Ryuji Mita Profile Picture
    465 on at

    Not sure if this is what you are trying to do, but since an enum is basically an Int value, the compiler will let you set Int values. As an example, the below code, though probably not recommended, works.

    ABC enumABCVariable;

    ;

    enumABCVariable = 1;

    // or

    enumABCVariable = global::enum2int(ABCModel::ContributionMargin);

    If "without using that EnumType" means to set the value of a different enum, the above code should do the trick to get you past the compiler.  Of course if a invalid value is set, you will probably get bad results at runtime...

  • Community Member Profile Picture
    on at

    I think that what Deepak M wanted to ask, was that in the need of using str2enum, how can we pass the first parameter without hardcoded enum type, but instead, by using some enum name or enum id (i.e. do it all in dynamic) ?

    I still wonder if it's possible...

  • Verified answer
    Community Member Profile Picture
    on at

    Maybe such an example might help ?

    static void TestEnum(Args _args)
    {
        str         label = "Ventes";   // French word for "Sales"
        EnumId      enumId = 281;       // My system Id for SalesPurch enum

        SalesPurch  salesPurchIn;
        SalesPurch  salesPurchOut;

        DictEnum    dictEnum = new DictEnum(enumId);
        int         numEnum;
        ;

        // Test 1: we have a variable of desired Enum type (salesPurchIn of type SalesPurch), we can use the str2enum function
        salesPurchOut = str2enum(salesPurchIn, label);
        info(int2str(salesPurchOut));

        // Test 2: we don't have any variable, but we know the EnumId
        numEnum = 0;

        while(numEnum < dictEnum.values())
        {
            if(label == dictEnum.index2Label(numEnum))
            {
                info(int2str(dictEnum.index2value(numEnum)));
                break;
            }

            numEnum++;
        }

        // Test 3: we can do it with name2label anyway...
        info(int2str(dictEnum.name2value(label)));

    }

  • Community Member Profile Picture
    on at

    Hi Sébastien Valente,

    Thanks for your reply. This one only I have expected.

    Thanks and Regards,

    Deepak M.

  • Community Member Profile Picture
    on at

    Good to know: you're welcome !

    Cheers

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans