15
Jan
09

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!

Share and Enjoy:
  • del.icio.us
  • Digg
  • Fark
  • Google
  • NewsVine
  • Slashdot
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz

8 Responses to “as3 casting issue”


  1. 1 Theo Jan 15th, 2009 at 6:50 pm

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

  2. 2 senocular Jan 15th, 2009 at 6:55 pm

    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. 3 Jason Langdon Jan 15th, 2009 at 6:59 pm

    @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. 4 Jason Langdon Jan 15th, 2009 at 9:45 pm

    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. 5 Nat Jan 16th, 2009 at 10:17 am

    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. 6 Jason Langdon Jan 16th, 2009 at 12:03 pm

    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. 7 Josh Tynjala Jan 16th, 2009 at 8:20 pm

    true != “true”

  8. 8 Jason Langdon Jan 17th, 2009 at 12:26 am

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

Leave a Reply




Categories

Recent Comments

Posts this month

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

Archives