Flex: Casting with AS vs Casting with Braces

On August 26, 2009, in Flex, by Anuj Gakhar

I gave a telephonic interview yesterday (yes, I am looking for new contract roles these days!) and this question came up. The interviewer asked the difference between Casting with Braces and Casting with the “AS” operator in Flex. I thought I could write a little example to demonstrate the difference between the two.

So, lets say we have a User class as below :-

[xml]

package classes
{
public class User
{
public var firstName:String;
public var lastName:String;
}
}

[/xml]

Lets first try to cast this User object into another datatype using the braces :-

[xml highlight=”9″]
<mx:Script>
<![CDATA[
import mx.controls.Label;
import classes.User;

private function init():void{
var u:User = new User();
var b:Label = new Label();
var newUser:User = User(b);
}
]]>
</mx:Script>
[/xml]

You see the variable newUser holds the casted object. However, as soon you run this code, you will get a run time error. Something like this:-

[xhtml]

Type Coercion failed: cannot convert mx.controls::Label@3366b0b1 to classes.User.

[/xhtml]

Now, lets try to do the same thing with the “AS” operator.

[xml highlight=”14″]
<mx:Script>
<![CDATA[
import mx.controls.Label;
import mx.controls.Alert;
import classes.User;

private function init():void{
var u:User = new User();
u.firstName = "Anuj";
u.lastName = "Gakhar";

// incompatible types
var b:Label = new Label();
var newUser:User = b as User;
if(newUser) {
Alert.show("Casting Done");
} else {
Alert.show("Null Value");
}

}
]]>
</mx:Script>
[/xml]

When you run this code, you will always get “Null Value” Alerts.

So what do we notice here? Casting with AS returns a NULL value if the 2 types are incompatible. Whereas, Casting with braces throws a run time error if the 2 types are incompatible.

So, depending on the kind of error handling you have in place and whether or not you have NULL value checks in place, you can decide which way you want to go.

Tagged with:  

4 Responses to Flex: Casting with AS vs Casting with Braces

  1. Ah. I’ve always used the “as” operator and was not aware of the difference. Good to know!

  2. Anuj Gakhar says:

    Yeah you normally don’t see the difference in day to day programming. its only in the interview such questions come up 🙂

  3. Elan says:

    Yes, that sounds like an interview question! But I like the way you explained it. Just using the errors to determine which is which.

  4. Neo A. says:

    “So, depending on the kind of error handling you have in place and whether or not you have NULL value checks in place, you can decide which way you want to go.”

    We want to go for clean coding, generally. If NULL values are checked right off the start, braces would most likely serve the purpose, but if one goes for something intricate and (in a ways) sophisticated approach, he’d do well with “as”. Sometimes a hybrid proves very useful though.

    Neo A.
    SpurPress

Leave a Reply to Elan Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: