Search This Blog

Wednesday 9 November 2016

Error: Getting Enum Value in Container and assigning it to the Enum variable.

I just stop in one of the scenarios in which I was trying get the enum value from container and assigning it to the enum variable by converting its type from string to enum.

The code compiles well but at the run time it throws exception.


Below is the code that was causing an issue.

static void Job26(Args _args)
{
    container _bRMAccountTypes;
    BRMAccountType bRMAccountType;
    ;

    _bRMAccountTypes = [BRMAccountType::Business];

    bRMAccountType = str2Enum(bRMAccountType, conpeek(_bRMAccountTypes, 1));

    info(strfmt('%1', bRMAccountType));
}

After running it got the following exception.













To resolve this issue the simple solution is.

Do not type cast the enum value that you are getting from container and assign it directly to the enum variable.

like this.

static void Job26(Args _args)
{
    container _bRMAccountTypes;
    BRMAccountType bRMAccountType;
    ;

    _bRMAccountTypes = [BRMAccountType::Business];

    bRMAccountType = conpeek(_bRMAccountTypes, 1);

    info(strfmt('%1', bRMAccountType));
}

It will work fine and show the correct results:


















Thanks
Muhammad Zahid.

No comments:

Post a Comment