as3 casting issue

Either I’m going slightly mad or casting using the “as” keyword works differently to the old way of casting.

Here’s some code to illustrate:

showFormItem = (((e.target as CheckBox).selected as String) == _local.displayTriggerValue[i]) ? true : false
Now this gives a different result to:
showFormItem = (String((e.target as CheckBox).selected) == _local.displayTriggerValue[i]) ? true : false

WTF?!

I'm pretty sure I've got my brackets in the correct spot.

Yhelp!

Be Sociable, Share!
Tagged with: ,
Posted in Flash

8 comments on “as3 casting issue
  1. Theo says:

    Yeah, the “as” operator yields null if the cast fails, the regular cast yields an error. It’s in the docs.

  2. senocular says:

    Constructor casting in some cases also involves a conversion function. For example, with String, String() is actually a conversion function – it’s doing more than casting. It will take a value and make it a string (usually by invoking the object’s toString() method).

  3. @senocular – thanks for the clarification on that. I am definitely after the conversion in this instance, and didn’t realise that the “as” operator wasn’t doing that for me.

    @Theo – hey, nice to see you on my little patch of turf and not in the Mate/asfusion forums ;-)

  4. Possibly as a reminder to myself rather than an attempt to illuminate those of you out there who have a solid grasp of as3 – defining a new Array with one numeric argument in the constructor will create an array with the specified number of empty placeholder elements. Yup, took me another 5 minutes there to figure out that Array’s are behaving as they’re meant to, I’m just having an off syntactical day! So if you want to add the number 1 to an new array do so like this

    new Array([1]);
    

    And please don’t ask why I even have to do this…

  5. Nat says:

    Why not just use the Use Array literals [] directly:

    var myArray:Array = [ 1 ];

    btw it’s actually recommended by Adobe also:
    http://opensource.adobe.com/wiki/display/flexsdk/Coding Conventions

    Do this:
    []
    Not this:
    new Array()

    And this:
    [ 1, 2, 3 ]
    Not this:
    new Array(1, 2, 3)

  6. Ahhh, true dat Nat ;-)

    I would normally use just the [] array literal, but was declaring this particular array amongst a bunch of others that were all using (apparently not optimally) new Array(…)

    I read the conventions link you posted and I quote, “Use the Array constructor only to allocate an array of a prespecified size, as in new Array(3), which means [ undefined, undefined, undefined ], not [ 3 ].” So out with new Array() (which to be fair is a bit more descriptive) and in with [] from hence forth.

  7. Errrr, actually true is equal to “true”. But it ain’t equal to “” ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posts this month
January 2009
M T W T F S S
« Dec   Feb »
 1234
567891011
12131415161718
19202122232425
262728293031  
Archives