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 :
Finance | Project Operations, Human Resources, ...
Answered

How to check C# Boolean Property is Null or not in X++ code ?

(0) ShareShare
ReportReport
Posted on by 225

Hello,

I have one Boolean property in C# class (Model). and I want to check if it is NULL or not in X++ code. How can I do this ? 

I tried in x++ like : classObject.property != NULL but it gives exception.

How to check for for in X++ for C# boolean type.

I have the same question (0)
  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Pratik Rami,

    Could you, please, share exception details?

    Do you know if the boolean property in the C# class is nullable (bool?) or not (bool)? It could be not nullable and therefore you need to compare it with false.

  • Verified answer
    Martin Dráb Profile Picture
    237,948 Most Valuable Professional on at

    It's a pity that you didn't mention what exception you got.

    But if the type is boolean, comparing it with null indeed wrong. Simply use if (classObject.property), or compare it with true or false literals or a boolean expression.

  • Pratik Rami Profile Picture
    225 on at

    Yes. It is nullable (bool?). Right now I have changed some implementation and not able to Reproduce the exception I got. But yes it is working properly by comparing with false and also working if I write if(classObject.property) as Martin suggested below.

    Thanks for the reply

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

    bool and bool? are actually very different things. Therefore you weren't right when saying that it's a Boolean property.

    • bool is a primitive data data (System.Boolean).
    • bool? is a generic structure (System.Nullable<System.Boolean>).

    Try  something like this in X .

    System.Nullable nullableBool = classObject.Property;
    if (nullableBool.HasValue)
    {
        boolean value = nullableBool;
    }
    
    // Or simpler
    if (classObject.Property.HasValue)
    {
        boolean value = nullableBool;
    }

    Let us know how it went; CLR Interop often struggles with more complicated cases and you can't be sure it work until you actually try 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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 464 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans