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

Boolean automatically being set to false after checking in ternary ,possibly a bug.

(0) ShareShare
ReportReport
Posted on by

Hi experts

In my code i m using a boolean & ternary check like shown below code snippet.

When my code executes boolean is set to false, then changes to true after 1st if , displays PASS (which means boolean is true ) from ternary , however 2nd if condition fails and it shows boolean isPass value to FALSE in debugger.

Wondering if   its a BUG which resets the value of boolean  to false when evaluated via ternary operators ?

Please suggest a fix.

class test
{

    boolean isPass = false;

    if (some condition )
    {
    isPass = true;    
    }

    isPass = true ? info("Pass") : error("FAIL"));  // Info Pass

    if(some condition && isPass)  // Will not execute as isPasss becomes FALSE
    {
    
    }
}

Actual code for runclass if you want to test this issue. If it is indeed a bug then do I get a reward....Just checking.... :-) :-) :-)

class rcternarybugcheck
{
    /// 
    /// Runs the class with the specified arguments.
    /// 
    /// The specified arguments.
    public static void main(Args _args)
    {
        boolean isPass;

        if (1==1)
            isPass = true;

        isPass = true ? info("Pass"):Error("Fail");

        if(1==1 && isPass)
            info("BUGpass");

        info(strFmt(enum2Str(isPass)));

    }

}

Thanks

Mav

I have the same question (0)
  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Mav,

    info and error methods have the return type Exception. Any reason you are assigning this to a boolean variable?

  • Mav Profile Picture
    on at

    Hi Gunjan,

    I am not assigning any value to Boolean.

    Code just checks Boolean.  if true throw an info and if false throw error. It should throw error or bp message if system does not like us to use it in that way.

    I don’t think that after ternanry line is executed the system should set Boolean to false

    thanks

    Mav

  • Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Mav,

    Should it not be like this? 

    isPass == true ? info("Pass"):Error("Fail");

  • Mav Profile Picture
    on at

    No Gunjan, that will give compile error .

    Please try the code , I have provided the snippet.

    This  is getting interesting.

    thanks

    Mav

  • Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Mav,

    The first part of the expression has to be a condition. So your condition in this code could be either "isPass == true" or "isPass". SInce your statement was "isPass =", the variable isPass was getting set to a value.

    We normally assign the outcome of the operator to a variable or pass the outcome to a method. In your case, I am able to assign this to a variable of type Exception

    Exception ex;
    
    ex = (isPass == true) ? info("Pass"):error("Fail");

    I could pass the outcome to info() method as well

    info((isPass == true) ? "Pass":"Fail");

  • Mav Profile Picture
    on at

    HI Gunjan,

    For your above suggested approaches . Below are the findings.

    Approach 1 is having same issue i.e. the boolean isPass gets automatically converts to false after executing the ternary line.

    Plus you will now have to declare another boolean to capture the value of boolean  which is an overhead.

    Approach 2   Has no Error condition so cannot be used as requirement is to throw info for true & error for false.

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi Mav,

    the bug is in your code, not in ternary operation. In your code you are assigning value to your boolean - you use = operator which assigns value.

  • Mav Profile Picture
    on at

    Hi Nikolaos

    That is very bug i am highlighting . Below line assigns the value to true.

    Then why is that after ternary operation is over i see the values set to False for boolean isPass

    Why does the compiler set the value to false when i m explicitly setting it to true (pls refer to full code above)

    isPass = true ? info("Pass"):Error("Fail");

    2ndly how do i check for boolean value in ternary  & based on boolean i throw info if true and error if false.

    ideally it should be

    isPass ==  true ? info("Pass"):Error("Fail"); but compiler gives error for that syntax.

    Setting additional boolean to evaluate another  boolean woluld lead to use of exta boolean, for below line i have to use extra boolean ex.

    and with this approach also you wil notice that ex which is set to True becomes false just after ternary opertaion is over.

    ex = (isPass == true) ? info("Pass"):error("Fail");

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    isPass = true ? info("Pass"):Error("Fail");

    Since the "true" condition is always true, this is same than:

    isPass = info("Pass");

    ..which doesn't make sense and doesn't compile.

    Also in your second code you end up assigning info("Pass") to the ex variable.

    What would you like to happen when you assign info("Pass") to a boolean variable? I think that the code doesn't make sense.

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

    No, you're not setting it to true. Let me show you what your code actually does.

    This code:

    isPass = true ? info("Pass"):Error("Fail");

    can be rewritten by uing if/else instead of the ternary operator:

    if (true)
    {
    	isPass = info("Pass");
    }
    else
    {
    	isPass = eror("Fail");
    }

    Let's ignore the 'else' branch, because it won't ever be called, and let's replace the call of info() with its body. We get this:

    infolog.add(Exception::Info, "Pass");
    isPass = Exception::Info;

    Are you assigning true to isPass? No, you're using true in the condtion and you're assigning Exception::Info to isPass.

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 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans