Question - and deeply embarrassed...

M

Mark Preston

I know... I should be able to figure this out. Its silly and its simple
but its driving me crackers. Don't tell me I should leave it a while and
come back to it - I know I should and it will probably be blatantly
obvious when I do. But at the moment its driving me nuts...

I have a pop-up, pop-down set of layers. They are turned on and off by
menu links. No problem, except that clicking a link for a layer when the
layer is already displayed switches it off. I'm trying to prevent that
with a simple, one-line, conditional:-

"layer" is the layer to switch on.
"set_layer" is the currently displayed layer.
"hideIt()" hides the specified layer.

My one-liner is:

if (!layer==set_layer) hideIt(set_layer);

Now why - and I know I should know this - does this condition never hide
a layer?
 
M

Michael Winter

On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston

[snip]
"layer" is the layer to switch on.
"set_layer" is the currently displayed layer.
"hideIt()" hides the specified layer.

My one-liner is:

if (!layer==set_layer) hideIt(set_layer);

Now why - and I know I should know this - does this condition never hide
a layer?

Because of the operator precedence. The negation operator has higher
precedence then the comparison operator. This means that the object
reference in 'layer' is logically negated and becomes a boolean. If the
reference is an object (and it probably is), '!layer' will evaluate to
false. If 'set_later' is is an object (and that probably is, too), it will
evaluate to true. True doesn't equal false, so hideIt() is never called
when it should be.

What you intended is either

if(layer != set_layer) ...

or

// Force the reference comparison first
if(!(layer == set_layer)) ...

Good luck,
Mike
 
J

Jeff North

| I know... I should be able to figure this out. Its silly and its simple
| but its driving me crackers. Don't tell me I should leave it a while and
| come back to it - I know I should and it will probably be blatantly
| obvious when I do. But at the moment its driving me nuts...
|
| I have a pop-up, pop-down set of layers. They are turned on and off by
| menu links. No problem, except that clicking a link for a layer when the
| layer is already displayed switches it off. I'm trying to prevent that
| with a simple, one-line, conditional:-
|
| "layer" is the layer to switch on.
| "set_layer" is the currently displayed layer.
| "hideIt()" hides the specified layer.
|
| My one-liner is:
|
| if (!layer==set_layer) hideIt(set_layer);

You have:
if( not layer==set_layer) hideIt(set_layer);

Remove the exclamation symbol from the statement.
| Now why - and I know I should know this - does this condition never hide
| a layer?

---------------------------------------------------------------
All I can say to the fundamentalist christians is:
BRING BACK THE LIONS BRING BACK THE LIONS
BRING BACK THE LIONS BRING BACK THE LIONS
BRING BACK THE LIONS BRING BACK THE LIONS

(e-mail address removed) : Remove your pants to reply
---------------------------------------------------------------
 
M

Mark Preston

Michael said:
On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston

[snip]

Because of the operator precedence.[snip]
Boom! (Sound of shooting own foot)...

I knew it was simple and I should have realised it. I missed out the
extra brackets...
 
M

Mark Preston

Mark said:
[snip]

My one-liner is:

if (!layer==set_layer) hideIt(set_layer);

Now why - and I know I should know this - does this condition never hide
a layer?

Thanks for all the help folks - its nice to know there is someone out
there when you have one of the inevitable "blonde moments" (as we call
them here in the UK). Wonder why it is we poor coders can spend eight
hours a day, five days a week doing this stuff and then one day just
forget how to do one particular line...?
 
J

Jeff North

| Mark Preston wrote:
|
| > [snip]
| >
| > My one-liner is:
| >
| > if (!layer==set_layer) hideIt(set_layer);
| >
| > Now why - and I know I should know this - does this condition never hide
| > a layer?
|
| Thanks for all the help folks - its nice to know there is someone out
| there when you have one of the inevitable "blonde moments" (as we call
| them here in the UK).

Hey!!! I resemble that remark :)
Yes I *am* blonde :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top