PDA

View Full Version : Boolean class


meddlingwithfir
10-27-2006, 07:39 PM
Maybe I'm not getting this.


var testTrue:Boolean = new Boolean("true");
var testFalse:Boolean = new Boolean("false");

Alert.show(testTrue.toString(), "testTrue"); // displays true
Alert.show(testFalse.toString(), "testFalse"); // displays true


Doesn't this just seem wrong?

I've looked at the documentation, and I see why it happens (http://livedocs.macromedia.com/flex/2/langref/package.html). Any non-empty string returns true. Which is a royal pain when grabbing values from ColdFusion CFC's. I build a function that returns a Boolean -- I call this function using the Flash Remoting option, and the ResultEvent that it comes back with creates the result as a String object, not a Boolean. So I have to create "lengthy" String comparison code to determine what I actually got back!


switch (event.result)
{
case "true":
isLoggedIn = true;
break;
default:
isLoggedIn = false;
break;
}


Maybe I'm just a complainer, but that global Boolean() function looks poorly written to me.

CDHBookingEdge
10-27-2006, 07:46 PM
Wow! That's *bleeped*!

dr_zeus
10-27-2006, 08:01 PM
I'm guessing that behavior is specifically defined by the ECMAScript standard from which Actionscript is derived.

I see a simple way to put your "lengthy" code into a single line:

isLoggedIn = event.result == "true";

meddlingwithfir
10-27-2006, 08:06 PM
Ahh, that could be true about the ECMA script. I'm not too knowledgeable about standards.

Still though, it irks me even having to do that calculation :mad:

I do like your comparison code -- It's quite elegant!

Tink
10-27-2006, 08:09 PM
It always been like that

http://livedocs.macromedia.com/flex/2/docs/00001827.html

Casting to Boolean from a String value returns false if the string is either null or an empty string (""). Otherwise, it returns true.

var str1:String; // Uninitialized string is null.
trace(Boolean(str1)); // output: false

var str2:String = ""; // empty string
trace(Boolean(str2)); // output: false

var str3:String = " "; // white space only
trace(Boolean(str3)); // output: true

meddlingwithfir
11-02-2006, 06:49 PM
I guess my gripe isn't so much that the Boolean can't accept a "false" string, but more that when my CFC returns a boolean, the EventResult stores it in a String object. Kind of annoying when communicating between Flex and my CFC's.