Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Previous section Next section Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Topic:
How can this be?
This thread has 7 replies. Displaying all posts.
Post 1 made on Saturday October 2, 2010 at 20:11
rap
Long Time Member
Joined:
Posts:
September 2006
59
Racking my brain for the better part of a day...

This code:

System.print("@Button ENTRY, zone2on: " + System.getGlobal(zone2on) + " w= " + w + "!w= " + !w);

System.setGlobal(zone2on, w); //this added for troubleshooting, no help

w = !w;

System.setGlobal(zone2on, w);

System.print("@Button- afte w=!w, zone2on: " + System.getGlobal(zone2on) + " w= " + w + "!w= " + !w);

Produces this output:
@Button ENTRY, zone2on: false w= false !w= false
@Button- afte w=!w, zone2on: false w= false !w= true

Yes, just a simple toggle function... The code is on a button, all is within one activity, the odd thing is upon the SECOND button press all behaves normally.
Post 2 made on Sunday October 3, 2010 at 02:32
alpha
Long Time Member
Joined:
Posts:
September 2003
258
Might not be fully defined before first use of button.
Project Boredom 2 is here. [Link: mediafire.com]
------------------------
Check Version 1 & 2 out in the files section.
Post 3 made on Sunday October 3, 2010 at 04:31
BluPhenix
Long Time Member
Joined:
Posts:
December 2008
371
System.setGlobal and System.getGlobal work only with strings as far as I know, you need to convert to bool prior to the:

System.print("@Button ENTRY, zone2on: " + System.getGlobal(zone2on) + " w= " + w + "!w= " + !w);

statement.

!w on a string is, I don't know ... anyway not what you expect.

So the behavior is normal.
Post 4 made on Sunday October 3, 2010 at 05:41
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Strings are really funny when cast to booleans. An empty string will evaluate to boolean false. A string with a single space (or non-empty string) will always evaluate to boolean true. This is by design and IIRC, is discussed in Flanagan.

In practice, you'd want something other than 'false' to evaluate to true. In this case, you can use the Boolean function or use the Boolean Wrapper function and use valueOf() call to the resulting object.

[Link: w3schools.com]

Then, try the tutorial and note the last line of the example output that adheres to my first statment. This is not quite what one would expect from the language but it is what it is.

[Link: w3schools.com]

The only real way to see if the value is 'true' is by string comparison.
(str == 'true')

You can make this a bit more flexible this a bit by doing:
(str.toUpperCase() == 'TRUE')
Lyndel McGee
Philips Pronto Addict/Beta Tester
OP | Post 5 made on Sunday October 3, 2010 at 12:17
rap
Long Time Member
Joined:
Posts:
September 2006
59
Thank you all. The references were very helpful. But, I have nearly identical code that works correctly in some locations but not else where. I'm putting a Zone2 status indicator on the System page. The initialization routine works perfectly, I can toggle it back and forth all day long. But when jumping to the home screen the red line does not evaluate correctly.

This is the toggle code:

w = !w;

System.setGlobal(zone2on, w);

So, zone2on should always have a Boolean true|false.

Display functon when state variable is not initialized

function display_zone2_status() {
z2ind = GUI.widget("ZONE_2_IND");  // zone2 "2" icon
z2unk = GUI.widget("ZONE_2_UNK");// unknown icon
if (w === null) {
  z2ind.visible = false;
  z2unk.visible = true;
 } else {
  z2ind.visible = (w ? true : false);
  z2unk.visible = false;
}
}

Display functon all pages
function display_zone2_status() {
z2ind = GUI.widget("ZONE_2_IND");   // zone2 "2" icon
z2unk = GUI.widget("ZONE_2_UNK");  // unknown icon
z2ind.visible = (w ? true : false);
z2unk.visible = false;
}
Post 6 made on Sunday October 3, 2010 at 12:59
BluPhenix
Long Time Member
Joined:
Posts:
December 2008
371
So, zone2on should always have a Boolean true|false.

As explained, zone2on in the Global space (setGlobal, getGlobal) does not have a boolean value, it is a string.

If i'm not mistaking, in javascript an uninitialised variable does not have a null value, but an undefined value. So w===null will not evaluate to true. Please correct me if I'm wrong.

If w is undefined, z2ind.visible = (w ? true : false); wlll not do anything.
OP | Post 7 made on Sunday October 3, 2010 at 13:11
rap
Long Time Member
Joined:
Posts:
September 2006
59
On October 3, 2010 at 12:59, BluPhenix said...
So, zone2on should always have a Boolean true|false.

As explained, zone2on in the Global space (setGlobal, getGlobal) does not have a boolean value, it is a string.

If i'm not mistaking, in javascript an uninitialised variable does not have a null value, but an undefined value. So w===null will not evaluate to true. Please correct me if I'm wrong.

If w is undefined, z2ind.visible = (w ? true : false); wlll not do anything.

OK, I missed the point that Global space only holds strings. I'll work through that.

On boot up I execute:
// INITIALIZE any state tracking variables on boot
// this call will generate a PS ActionList error which is "normal" due to it jumping out of the current activity
if (System.getGlobal(zone2on) === null) {
scheduleAfter (100, CF.widget("GOTO", "SAVE_STUFF_HERE").executeActions()); // set zone2/outdoor zone
}

In this case the operation evaluate to true and the jummp takes place as expected.

Thank you,
Post 8 made on Sunday October 3, 2010 at 13:44
BluPhenix
Long Time Member
Joined:
Posts:
December 2008
371
Yes, the

System.getGlobal(zone2on) === null

evaluates as true because the variable doesn't exist, it's not that it exists but is uinitlialised.

So if the variable doesnt exist -> null
if uninitialised -> undefined


Jump to


Protected Feature Before you can reply to a message...
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now.

Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.

Hosting Services by ipHouse