Lesson 7 : Replacing casting operator ( ) with 'as' operator

37 views
Skip to first unread message

Paul

unread,
Apr 22, 2010, 8:54:22 PM4/22/10
to Flex 3:Training from the Source
In Lesson 7, in the CategorizedProductManager.mxml, in function
prodByCategoryHandler, when creating the Product object, instead of
using the casting operator ( ) , I use the 'as' operator.

This caused the categorizedProducts to be filled with null values with
no error.

I did abit of check in the differences between using ( ) or 'as' and
it tells me that using 'as' is recommended since it doesn't throws any
TypeError but fill the Product object with default values for the
relevant type, whereas using ( ) will throw a TypeError if flex fails
to parse the data.

I would like to know whether the 'as' operator is easily switchable
with the ( ) operator when doing casting.

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Flex 3:Training from the Source" group.
To post to this group, send email to flex-3training-...@googlegroups.com.
To unsubscribe from this group, send email to flex-3training-from-t...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/flex-3training-from-the-source?hl=en.

Michael Labriola

unread,
May 4, 2010, 8:19:41 AM5/4/10
to Flex 3:Training from the Source

If you are just casting the two methods are identical. However, the as
method is just a cast not a conversion

'3' as Number will fail because '3' is a string
Number( '3' ) will work because it is casting and converting

Also, that latter syntax is often confused with object creation..
there is a huge difference between

new Product( .... ) and
Product( ... )

Mike
Reply all
Reply to author
Forward
0 new messages