CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2011
    Posts
    4

    Question Conditional operator (==) returning false even the condition is true ?

    I have created three property pages on propertysheet,and intializing a bool Value on the parent class of the propertysheet .Now, When accessing that Bool value under InitDialog()Second property page it always return TRUE and while comparing that value in the if statement ,Condition returns false. But if the same procedure, when repeated on third property page Everything works fine.

    //My code
    //CPage is Parent of propertysheet
    // BtnState is Bool value initialized on CPage of Propertysheet
    BOOL CGs::OnInitDialog()
    {
    CPropertyPage::OnInitDialog();
    CPage * m_pg = ((CPage*)((CGSPropertySheet*)GetParent())->GetParent());
    if(m_pg->BtnState == true)
    {
    m_set.EnableWindow(false);
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Conditional operator (==) returning false even the condition is true ?

    replace "if(m_pg->BtnState == true)" with "if(m_pg->BtnState)"; the reason why it doesn't work is that the boolean constant on the rhs is promoted to the integral value used to represent BtnState, that does not necesserely equal BOOL(true).
    Last edited by superbonzo; September 2nd, 2011 at 07:22 AM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Conditional operator (==) returning false even the condition is true ?

    1) There is no way we can verify your claims. Instead, save the results to a variable and inspect what that value is:
    Code:
    BOOL value = (m_pg->BtnState == true);
    if ( value ) // value is not 0
    2) BOOLs are not bools. The former is a Windows constant, the latter is a C++ type. You should be comparing a BOOL value to TRUE, not true. If you did this, then you wouldn't have run into the issue that superbonzo mentioned. No promotion rules would have occurred, and you would be comparing apples to apples.

    3) Use code tags when posting code.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Sep 2011
    Posts
    4

    Lightbulb Re: Conditional operator (==) returning false even the condition is true ?

    Thanks Paul and Supermonzo for reply....i am still confuse not getting result...guide me more !!

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Conditional operator (==) returning false even the condition is true ?

    Quote Originally Posted by sunny chouhan View Post
    Thanks Paul and Supermonzo for reply....i am still confuse not getting result...guide me more !!
    What's your current code?

  6. #6
    Join Date
    Sep 2011
    Posts
    4

    Lightbulb Re: Conditional operator (==) returning false even the condition is true ?

    Refer above for the code.......

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Conditional operator (==) returning false even the condition is true ?

    Quote Originally Posted by sunny chouhan View Post
    Refer above for the code.......
    So you haven't changed it to incorporate the suggestions so far? Why not?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Conditional operator (==) returning false even the condition is true ?

    Quote Originally Posted by sunny chouhan View Post
    Refer above for the code.......
    You were already told the above code was wrong and why it was wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured