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!

Please follow and like us:

8 thoughts on “as3 casting issue”

  1. 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).

    Reply
  2. @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 😉

    Reply
  3. 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…

    Reply
  4. 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.

    Reply

Leave a Reply to Theo Cancel reply