Apr
08

Flash casting weirdness

5 comments Posted by: Nahuel

Now that we have strict typing in ActionScript 2.0, the compiler complains whenever we try to assign a value to a variable that doesn't match its expected type. That is easy to see in the following example:

I made a class ( Container ) that holds any kind of data. Since it is very generic, it only knows that it holds an Object.
This class also has a method ( getItem() ) that returns that data. So if we try to assign the return value of getItem() to a reference of a type other than Object, the compiler will complain:

var c:Container = new Container(new Array());<br />
var i:Array = c.getItem();

the compiler will throw an error like this:
Type mismatch in assignment statement: found Object where Array is required. var i:Array = c.getItem();

So the solution for this is to cast the returning data to the class that we need. We have two options:

Option 1 var i:Array = Array(c.getItem());<br />
Option 2 var i:Array = (Array)(c.getItem());

Both works very well except when you try to cast to your own classes, not the built-in ones. To show that, I made another class ( Item ) and a test class ( TestCasting ) that shows this behavior:

Option 1 var i:Item = Item(c.getItem());<br />
Option 2 var i:Item = (Item)(c.getItem());

The first option runs without a problem, but the second doesn't. It simply assigns "undefined" to the variable without the compiler complaining. So it's obviously better to avoid the second option. I used to do it that way because it always works in Java, but not in Flash. You never know when these little things will jump on you.

For a better understanding, I put this example in a zip to download.

Category: Flash |

5 Comments so far

Write yours
Cesar Tardaguila
I've found som strange issues when casting too.

There was one strange behaviour when casting to array, that I'm not able to reproduce right now, but I'll try to remember.

Anyway, some times, you can loose a lot of time because of these little things, so thanks for sharing.
Nahuel
2. Nahuel wrote on April 09, 2004 at 10:38 AM
Hi Cesar
You are totally right, I spent like an hour trying to debug an application and trying to understand this behaviour.
Thomas
hi,
this post is a little dated, but the problem still exists. i think i've found the reason, unfortunately no solution (in the case of array):

http://www.blog.lessrain.com/?p=203
Trout
I just ran into the array casting problem and found this article. Since I couldn't find a solution, anywhere, here's what I came up with:

var tmp_obj = c.getItem(); // Get item, and auto cast to an un-typed variable
some_arr = tmp_obj; // Assign to array object

jd
Nice tutorial, very useful.

Leave your comment

Comment etiquette: As a gesture to those subscribed to this post, please keep your comments relevant to the post.

Your email address will never be displayed.
Email is gravatar enabled.Gravatar are the pictures you see next to the comments. If you like to have one, visit gravatar



Allowed tags:

<code>
All other tags will be shown as such, when in doubt, use the preview.




Preview:

Refresh Preview
1. You wrote on