Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

(0) ShareShare
ReportReport
Posted on by 1,433

I like when I do upgrades to my Dev box to go through the error list warnings to see what has been made obsolete or deprecated on code that we have custom on

Some of the warnings I get are:

1) Warning Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results. Extensible enum values are not deterministically assigned to a numerical value. 

2) Warning Cast from extensible enum 'Extensible Enumeration(SalesStatus)' to 'int' potentially harmful and deprecated.

3) Warning Cast from extensible enum 'Extensible Enumeration(EcoResReleaseProductToCompany)' to 'int' potentially harmful and deprecated.

The above warnings are on code that looks correct to me and I have seen it used exactly the same in standard code. So why do I get the warning.

For example:

1)  Extension:  "if (!discAdmSearchCustomer.AccountRelation && discAdmSearchCustomer.AccountCode ==  PriceDiscPartyCodeType::Table)" generates the warning mentioned in (1).

If you look in standard code PriceDiscAdmSearch.procesCust(), you will see it is used exactly like in my extension:

Standard: 

i"f (!discAdmSearchCustomer.AccountRelation && discAdmSearchCustomer.AccountCode == PriceDiscPartyCodeType::Table)
{
return;
}"

2)  In my data entity postload() method I have:

"

SysDictEnum SalesStatusEnum = new SysDictEnum(enumNum(SalesStatus));

this.SalesStatus    = SalesStatusEnum.value2Label(salesTableLocal.SalesStatus);"

In https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/system-classes/dictenum-class, one should use dictenum::Value2Label. However  intelliSence is only showing DictEnum::Value2Id.

So how should I write the code to get rid of the warning?

3) My custom:

itemCreateArgs = new Args();
itemCreateArgs.record(ecoResProduct); 
itemCreateArgs.parmEnumType(enumNum(EcoResReleaseProductToCompany)); 
itemCreateArgs.parmEnum(EcoResReleaseProductToCompany::Yes); 

If you look in standard form: RetailAffiliationPriceGroup, method jumpRef(), you will see that parmEnumType is being used exactly like my custom:

Args args = new Args();
args.parmEnumType(enumNum(RetailDiscountOfferTypeBase));
args.parmEnum(enum2int(RetailPeriodicDiscount.PeriodicDiscountType));

Any suggestions on how to get rid of these warnings is much appreciated

  • Retha Profile Picture
    Retha 1,433 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    " AdjustmentTypeCtrl.selectText(enum2Str(adjClass.parmType())); " doesn't work. It doesn't give an error at compile time and it takes the warning away, but it is not change the selection value to what was previously used. adjClass.parmType() has the correct value of what SSMS is stating the value is for the base enum element.

    So I switched back to "AdjustmentType.selection(adjClass.parmType());" and will just have to live with the warning message. 

  • Retha Profile Picture
    Retha 1,433 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    Hi Sukrut

    For PricediscProductCodeType I have an extension.

    I fixed the other problems for #2, #3 and #4 and did again a build but this time only my custom model and there are no warnings for #1 anymore.

  • Retha Profile Picture
    Retha 1,433 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    For #4, I came across standard form PriceDiscAdmSearch with a combobox control TmpPriceDiscAdmSearchCust_PriceGroupType. The modified method shows that it's using the valueStr() instead of the Selection() property. And then it compares it in a switch statement with enum2str.

    So I changed the init-method to:  AdjustmentTypeCtrl.selectText(enum2Str(adjClass.parmType()));

    And to pass it back to the class in the closeOK() method, I did: 

    AdjustmentTypes typeSelected;
    typeSelected = str2Enum(typeSelected, AdjustmentTypeCtrl.valueStr());
    adjClass.parmType(typeSelected);

    I'm a bit worried about the above that it may return potentially the wrong value for the enum. In our case the language is set to 'en-US', so it should theoretically be ok: https://docs.microsoft.com/en-us/previous-versions/dynamics/ax-2012/reference/aa655065(v=ax.60)?redirectedfrom=MSDN

    I can also do any2enum: https://docs.microsoft.com/en-us/previous-versions/dynamics/ax-2012/reference/aa596893(v=ax.60)

    AdjustmentTypes typeSelected = any2Enum(AdjustmentTypeCtrl.selection());

    adjClass.parmType(typeSelected);

    However because AdjustmentTypes is a custom base enum, I couldn't type in a value, only a name and label at time of creation of the base enum.

    When I look at my base enum in SSMS, then I can see that a numeric value was assigned to the "value" property. 

    One would assume that when a base enum is assigned to a combobox that the backend will create it as :

    ComboBox a text = enum.label and value = enum.value as seen in SSMS. But I don't know if this assumption is correct. 

    Maybe it's wrong, thus the reason for the warning when using the selection() property the way I did in #4.

    Which means for combobox using a standard base enum type, any2Enum should work, but for custom base enums or where a standard enum was extended by custom and one cannot enter a "value", one should rather use str2Enum and hope for the best.

    Does this approach sounds correct or has anybody other suggestions?

  • Sukrut Parab Profile Picture
    Sukrut Parab 71,671 Moderator on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    for #1 those warning are coming for standard enums or you extended those enums and using in your code ?

  • Retha Profile Picture
    Retha 1,433 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    Sergei, I just upgraded my Dev box to v10.0.13. It is the latest  version that is generally available.

    So for now I can ignore #1 until I can upgrade to v10.0.14 when it becomes generally available.

  • Retha Profile Picture
    Retha 1,433 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    4) Another one I'm trying to figure out is where I have in a form a combobox which is of type Enum.

    I then take the selection and pass it to a class. I also pull the value in the lass and assign it to the selection of the combobox to indicate what the value was the user selected previously.

    This is a typical example of an action menu item when the action is driven by the class which has a dialog form to prompt users for parameters to be used in the runOperation of the class.

    My enumtype is called AdjustmentTypes and it has values:

    Percentage, Amount, quantity

    The class has a Get/Set method of enum type: AdjustmentTypes

    The form control has autoDeclaration = YES.

    Form control's name is AdjustmentTypeCtrl.

    In the Init() method of the form I assign the default value as found on the class:

    AdjustmentTypeCtrl.selection(adjClass.parmType());

    This gives me the warning: Cast from extensible enum 'Extensible Enumeration(AdjustmentTypes)' to 'int' potentially harmful and deprecated

    On closeOK() method of the form, I want to take the selection pass it to the class .

    adjClass.parmType(AdjustmentTypeCtrl.selection());

    However it generates the warning:

    Assigning a numerical value to extensible enum 'Extensible Enumeration(AdjustmentTypes)' will yield unexpected results. Extensible enum values are not deterministically assigned to a numerical value.

    The class parameter is defined as:

    AdjustmentTypes vAdjType;

    public AdjustmentTypes parmType(AdjustmentTypes _type = vAdjType)

       {

           vAdjType = _type;

           return vAdjType;

       }

    The combo box control on the form is a FormComboBoxControl and is defined as:

    AutoDeclaration = true

    Enum type = AdjustmentTypes

    Name = AdjustmentTypeCtrl

    Any idea how I can get passed the warnings in this case?

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    Hi Retha,

    Which version are you using? Quickly checked case #1 in 10.0.14 and I don't see any BP warnings in extensions with your code.

  • Retha Profile Picture
    Retha 1,433 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    Hi Gunjan

    #1 is the standard table: TmpPriceDiscAdmSearch       discAdmSearchCustomer;

    same as the standard code

    #2: I have changed it to enum2str() and now the warning is gone.

    #3: I changed the parmEnum to: itemCreateArgs.parmEnum(enum2int(EcoResReleaseProductToCompany::Yes)); and now the warning is gone.

  • Verified answer
    Gunjan Bhattachayya Profile Picture
    Gunjan Bhattachayya 35,421 on at
    RE: Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results

    Hi Retha,

    For #1, does "discAdmSearchCustomer" refer to the same table as in the example you provided, or is it  new table? In case it is a new table, Please check if the field has the same enum/EDT assigned to it.

    For #2, I think you can use enum2Str(). It should return the label of the enum value.

    For #3, the standard code hss enum2int() in the parm call whereas in your custom code, you have the enum value specified in the parm method. Have you tried the "enum2int()" here as well and check if that takes care of the warning?

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans