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 : falseNow 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:
Yeah, the “as” operator yields null if the cast fails, the regular cast yields an error. It’s in the docs.
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).
@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 😉
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
And please don’t ask why I even have to do this…
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)
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.
true != “true”
Errrr, actually true is equal to “true”. But it ain’t equal to “” 😉