Skip to main content

Notifications

Announcements

No record found.

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

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,340 Most Valuable Professional on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    I already explained it in detail above - please scroll up and read it.

    No, the value should't be true. The system is not brokem; the problem is in your expectations. You're not assigning 'true'; you're assigning Exception::Info. Which is interpreted as 'false'.

    'true' in your statement isn't used for assignment - it's used for decision whether you assign the return value of info() or error(). Please see previous answers for more details.

  • Mav Profile Picture
    Mav on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    Please share why is that assignment does not change to true in below code, it should change to true correct ?

    pastedimage1615842636052v1.png

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,340 Most Valuable Professional on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    The ternary operator is used to assign one of two values. If you don't want any assignment, using the ternary operator doesn't make a good sense. Simply use if/else. For example:

    if (isPass)
    {
    	info("Pass");
    }
    else
    {
    	throw error("Fail");
    }

  • Mav Profile Picture
    Mav on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    Ok so in that case how would you use ternary to throw info when boolean is true and error when boolean is false.

  • Martin Dráb Profile Picture
    Martin Dráb 230,340 Most Valuable Professional on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    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.

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    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.

  • Mav Profile Picture
    Mav on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    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
    nmaenpaa 101,156 on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    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
    Mav on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    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.

  • Gunjan Bhattachayya Profile Picture
    Gunjan Bhattachayya 35,421 on at
    RE: Boolean automatically being set to false after checking in ternary ,possibly a bug.

    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");

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! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,340 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans