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)

Ternary Operator in AX 2012

(0) ShareShare
ReportReport
Posted on by

Can anyone explain the strange behavior I'm seeing with the ternary operator in AX 2012?  I have a line of code like this:

int numLines;

numLines = (myCLRArray != null) ? myCLRArray.get_Length() : 0;

This used to work in AX 2009 but in AX 2012 I get the following warning from the compiler:

Operand types are not compatible with the operator.

If I change the code to use a normal If/Else, the compiler does not complain:

int numLines;

if(myCLRArray != null)

numLines = myCLRArray.get_Length();

else

numLines = 0;

I have a lot of these ternary operators in the code that I'm porting from AX 2009 so I'm curious if anyone knows what's wrong with using the ternary operator like am trying to.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    640579632b5c4e6780a624c4771b6f93 Profile Picture
    4,730 on at

    msdn.microsoft.com/.../cc584291.aspx

    When you work with .NET primitive types in X++ code, you can use the X++ equal sign (=) assignment operator. However, no other operators can be used with CLR primitives. For instance, you cannot use the comparison operators (such as == or >). Also, you cannot use bitwise operators (such as & or |).

    For more information about X++ operators with .NET primitive types, see Operators for CLR Primitive Types

  • Suggested answer
    640579632b5c4e6780a624c4771b6f93 Profile Picture
    4,730 on at

    This should work

    numLines = (myCLRArray != null) ? myCLRArray.get_Length() : System.Convert::ToInt32(0);;

  • Greg's Mom Profile Picture
    on at

    Thanks for your helpful reply Huzaifa!

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