Search code examples
axaptadynamics-ax-2012

Dynamics ax 2012 compiler warning when seting smmOpportunityTable to common


A class can receive either a VendTable, CustTable or smmOpportunityTable record.

For one instance a CustTable record was usually received, and a new requirement brings an smmOpportunityTable into play, so I’m using a common record to catch it with this piece of code:

(some other stuff happens earlier in the code to set the custTable record).

commonParty = custTable.RecId != 0 ? custTable : smmOpportunityTable;

The problem is, the above line of code gives a compile warning “Operand types are not compatible with the operator.”; faulting on the smmOpportunityTable.

My question, why can’t I set an instance of smmOpportunityTable to common? Surely its of base type common? Any ideas how to resolve the warning?

I’m developing in Dynamics Ax 2012 R1.


Solution

  • The warning is actually just because the compiler seems to be incorrectly handling the use of the ternary operator.

    To remove the warning, you can just rewrite as:

    if (custTable)
        commonParty = custTable;
    else
        commonParty = smmOpportunityTable;
    

    if (custTable) is the same as if (custTable.RecId != 0). It literally just checks if the RecId field is populated.

    And I might be confused with what you're trying to do in code, but for the other side of the assignment the convention is usually as follows:

    switch (common.TableId)
    {
        case tableNum(CustTable):
            custTable = common as CustTable;
            break;
    
        case tableNum(smmOpportunityTable):
            smmOpportunityTable = common as smmOpportunityTable;
            break;
    
        default:
            throw error(strFmt(Error::wrongUseOfFunction(funcName())));
    }