Converting an ActionScript Value to Boolean

In a typical week, I’ll work with ActionScript, Visual Basic, OpenScript, JavaScript, and perhaps even InstallScript. One of the possible traps of moving between languages is making assumptions that similar functions in different languages will in fact operate the same way. One recent example that bit me was in working with a Boolean (true/false) value in Flex and ActionScript. The issue as in a simple confirmation as to whether it was OK to exit the lesson. The design was to call this JavaScript function in order to get a standard “confirmation” dialog from the browser:

confirmClose = function (message) {
    return window.confirm(message);
}

Here is how we called it from ActionScript:

var returnString:String = ExternalInterface.call("confirmClose", exitMessage);

So far, so good. But how do we convert the string returnString into a Boolean value? In other words, returnString will be either “true” or “false.” My mistake was to use the syntax:

var okToExit:Boolean = Boolean(returnString);

You would think this would work since the ActionScript Number() function will take a numeric string and convert it to a number. Similarly, the Visual Basic CBool() function will take “true” or “True” and convert it to the Boolean True. However, this “casting” function returns true for both “true” and “false!” So the user exited even if she clicked the “Cancel” button in the dialog. This is NOT what we wanted. If I had read the Flex documentation, this behavior is clearly stated:

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

So what was the fix? Easiest for me was to create a ConvertToBoolean function that operated like I expected. The new syntax plus the function is shown below.

var okToExit:Boolean = ConvertToBoolean(returnString);

public function ConvertToBoolean(inputVal:String):Boolean {
    var returnVal:Boolean;

    if (inputVal == "true") { // need to check string since Boolean() looks for 1 or 0
        returnVal = true;
    }
    else {
        returnVal = false; 
    }

    return returnVal;			
}
Advertisements

About Jeff Rhodes
Jeff Rhodes is the Chief Technical Officer and owner of Platte Canyon Multimedia Software Corporation, a leader in developing commercial software that Improves the Lives of Training Developers. He graduated at the top of his class at the Air Force Academy, where he earned a Bachelor of Science in Electrical Engineering. Jeff received a Masters degree in Economics from the London School of Economics, which he attended under a British Marshall Scholarship. Jeff is the author of "Programming for e-Learning Developers: ToolBook, Flash, JavaScript, & Silverlight" and "VBTrain.Net: Creating Computer and Web Based Training with Visual Basic .NET." He also co-wrote "The ToolBook Companion." He has had numerous articles on training development published and is a frequent presenter at conferences both in the U.S. and Europe. Jeff lives in Colorado Springs with his wife Sue and sons Derek and Michael.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: