Flex3 casting

Does anyone know the real difference between the two casting methods in Flex 3?

var myObject1:MyObject = variable as MyObject; var myObject2:MyObject = MyObject(variable); 

I prefer to use the second method, because it will generate an error if the type fails, while the first method will simply return null. But are there any other differences? Perhaps any advantages in using the first method?

+4
source share
4 answers

The second type of casting has a different behavior for the top level ( http://livedocs.adobe.com/flex/2/langref/ ), for example. An array (obj) is not defined in the simple way that you describe; it creates a new Array, if possible, from obj, even if obj is an array.

I am sure that times that caused unexpected behavior would be rare, but I always use the “how” for this reason. That means if I do

 int(str) 

I know that this is a cast in the "attempt to transform" the meaning of the word not in the sense of "I promise it."

ref: received confirmation from this http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/

+5
source
  • The as method returns null if a failure occurs.
  • Method () generates errors and errors if a failure is performed.

If the variable value is not compatible with MyObject , myObject1 will contain null and you will be surprised by a null pointer error (1009: it is impossible to access the property or null object reference.) Somewhere later in the program when you try to access it. If you cast using MyObject(variable) syntax, you will get a type constraint error (1034: "Unable to convert type: cannot convert _ to _") on the same line - which is more useful than getting 1009 somewhere later and wondering what went wrong.

+2
source

I think somewhere on this site I read somewhere that as little faster than () , but I can not find the question again.

Besides asking this question many times, you will find a more detailed answer here .

I recently found a very useful tag [] when searching in StackOverflow, it allows you to search only on topics tagged with the specified tag (s). That way you can do a search like [actionscript-3] like vs cast . Here are some search tips: https://stackoverflow.com/search .

And no; the irony is that I can not find a question about performance and write about how to search, not lost on me;)

+1
source

I think it returns the base class, not null when casting is done, and () throws an error.

0
source

All Articles