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 use a Enum in switch case

(0) ShareShare
ReportReport
Posted on by

 Hi everyone

I have a dialog that has a Enum based on that I need to perform operations on data.

can you help me create a switch case which uses Enum as input.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mea_ Profile Picture
    60,284 on at

    Hi RamPrasad,

    Here is code from msdn article https://msdn.microsoft.com/en-us/library/aa629483.aspx

    switch (myEnum)
    {
        case ABC::A:
        ...
            break;
        case ABC::B:
        ...
            break;
        case ABC::C:
        ...
            break;
        default:
        ...
            break;
    }
  • startax Profile Picture
    1,845 on at

    Hi Ram Prasad,

    This Code is used to import data from CSV you can put your enum value in switch().

    switch (str2enum(campaignStatus,strLRTrim((conPeek(readcon,3)))))

                   {

                       case smmCampaignStatus::Planning:

                           campaignTable.CampaignStatus = smmCampaignStatus::Planning;

                           break;

                       case smmCampaignStatus::FollowUp:

                           campaignTable.CampaignStatus = smmCampaignStatus::FollowUp;

                           break;

                       case smmCampaignStatus::Cancelled:

                           campaignTable.CampaignStatus = smmCampaignStatus::Cancelled;

                           break;

                       case smmCampaignStatus::Closed:

                           campaignTable.CampaignStatus = smmCampaignStatus::Closed;

                           break;

                       case smmCampaignStatus::InProcess:

                           campaignTable.CampaignStatus = smmCampaignStatus::InProcess;

                           break;

                       case smmCampaignStatus::Response:

                           campaignTable.CampaignStatus = smmCampaignStatus::Response;

                           break;

                   }

  • startax Profile Picture
    1,845 on at

    Hi Ram Prasad,

    check this link for dialog that has enum value

    howtoax.com/dialogenumcombobox-class-in-ax-2012

  • Community Member Profile Picture
    on at

    It worked Thanks

    but I tried a different method I

    took a integer variable and assigned enum from dialog to it and used switch on that varible

  • startax Profile Picture
    1,845 on at

    Hi Ram Prasad,

    You can check this peace of code that might be help

    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

       numEnum = 0;

       while(numEnum < dictEnum.values())

       {

           if(label == dictEnum.index2Label(numEnum))

           {

               info(int2str(dictEnum.index2value(numEnum)));

               break;

           }

           numEnum++;

       }

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

       salesPurchOut = str2enum(salesPurchIn, label);

       info(int2str(salesPurchOut));

       // Test 3: we can do it with name2label anyway...

    for more information check below link

    howtoax.com/how-to-get-integer-variable-and-assigned-enum-from-dialog-in-ax-2012-using-x

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    RamPrasad: It's true that enums are actually integers under the hood, but is using numbers instead of enums really better? Using numeric constant used to be the practice in older programming languages, before enums were invented to make code easier to write, verify at compile time, make it more readable and so on.

    That there is another solution doesn't always mean that it makes sense to use it.

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