• Advertisement

Archived

This topic is now archived and is closed to further replies.

MSDN Sample code

This topic is 6354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got this following code from a MSDN sample.. BOOL bVisible = ((m_wndNavDialogBar.GetStyle() & WS_VISIBLE) != 0); pCmdUI->SetCheck(bVisible); OK.. In the first line, why do they have that != 0 part in the end? I could not understand what's the point. Edited by - jho on 8/31/00 1:47:39 PM

Share this post


Link to post
Share on other sites
Advertisement
Probably just someguy''s idea of making code more readable. The other thing is that (m_wndNavDialogBar.GetStyle() & WS_VISIBLE) is a long int or something, and the != will ensure that it''ll be typecasted properly into BOOL. Or else they really wanted it to be 1, intead of wherever the WS_VISIBLE flag is... who knows?


random-nomad

[still searching]

Share this post


Link to post
Share on other sites
If you want you can just ignore that. It basically does the same thing when you leave it without the != 0 so you can just forget about what you saw in there. It makes it clear to newbies, and the compiler itself what you''re doing. Some languages such as Pascal will require you to have that != 0 ( or <> 0 in Pascal) in order to compile. It''s a good coding style and makes it clearer. Wow, I just said the same thing twice

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Because doing a bitwise-AND on a value will either return 0, or any positive value. In your example, if the WS_VISIBLE flag WAS on, it would return the value of WS_VISIBLE, hence your BOOL value would be equal to WS_VISIBLE (not 1 or 0).

Then,if you said:

if (bVisible == TRUE)
{
// process code
}

it would NOT process the code, because TRUE is equal to 1, and bVisible wouldn''t be.

Share this post


Link to post
Share on other sites
Yes, those explanations are correct. That''s why I said it will "BASICALLY" do the same thing

quote:

Then,if you said:

if (bVisible == TRUE)
{
// process code
}



but then again, if you were to say

if ( bVisible )
{
// process code
}

it WILL process it correctly

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
quote:


but then again, if you were to say

if ( bVisible )
{
// process code
}

it WILL process it correctly




True (no pun intended), which is why I never use TRUE to verify my bool values =)

Share this post


Link to post
Share on other sites
I figured it out, Mr Random-Nomad is correct.

Getstyle() & WS_Visible are both DWORD,

BOOL is a byte I think,

Who knows what different compiles to do when you go BOOL x = Some_DWORD. Maybe it just gets the Least significant 8 bits to fit into the BOOL, then if you have a DWORD like 11...11100000000 then you''ll get the wrong answer.

So it is actually not just for clarity but actually safety.

Share this post


Link to post
Share on other sites
that''s my man =)

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
jho, it''s a lot more complicated than that. The only thing you need to know is that BOOL is defined as an INTeger so it doesn''t really matter. If the DWORD value contains ANYTHING at all, and even if the bool value was assigned the LSB(least significant bit), it would be 1 no matter what. So that means it''s true. That''s why when you say if ( something ) { /* do whatever */} it works, because it looks at the LSB, and if the value was for example 57 (binary = 0111001) the LSBwould be one, so the if() statement evaluates to true. BUT!!! if you say if ( something == TRUE){} or if ( something == 10 }{} then it doesn''t check for boolean values, but instead works with numbers and does a comparing between the two (and not simply test if it''s true or not), which is why if ( something == TRUE ) will return false if something does not equal TRUE (or 1) the way it is with the GetStyle() & WS_VISIBLE thing. if WS_VISIBLE is not equal to TRUE then it wouldn''t work. They''re both constants, and are numbers, so it does a number check (compare). I just hope you understood what I just said. If not come and read it in a week or so and you will

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Yes I understand exactly what you mean.. it simply maps arbitary numbers to 1 and 0 for safty.. like your If (...) sample

Sorry I just found out that
bool is 1 byte
BOOL is 4 bytes

Just starting to do some real windows programming just trying to make sure I know all these little basics

Share this post


Link to post
Share on other sites
BOOL is a Microsoft definition in VC whereas bool is a predefined type.

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
dsaf

Share this post


Link to post
Share on other sites

  • Advertisement