PDA

View Full Version : Things you feel silly for not realizing sooner...


drexle
08-24-2007, 04:52 PM
Hello,

Have any of you had any moments where you realize or discover the answer to something that has eluded you for a long time and in retrospect it seems so simple that it makes you slap your forehead? I just had a total *d'oh!* moment this morning as I was reading about flash and hashes. I have long wondered why there wasn't a hash variable type in actionscript, but since I was never really a programmer before getting into AS I didn't really miss having access to them, and have been working without the concept of associative arrays in my coding.

I just read however, about how you can use Object (or even an Array) as though it were a hash by doing this:


var a:Object = new Object(); // or Array, it really doesn't matter
a["Mon"]="Monday";
a.Teu = "Teusday";
trace(a[0]+" "+a[1]);
trace(a.Mon+" "+a.Teu);
trace(a["Mon"]+" "+a["Teu"]);


What kills me is that I really should have known this. I've done similar things to objects in the past without really realizing that I was essentially making a hash. What also kills me is that very recently I was building code that checks the typeof() variable found inside of an array location, and I wanted several "if else" branches to handle Strings, Numbers, and Arrays... well when I tried to detect for typeof()=="Array" I kept getting error. The reason? Flash considers Arrays to be typeof()=="Object".

Heh. Needless to say, I feel silly right now. Anybody else have moments like these lately?

Colin Campbell
08-24-2007, 06:13 PM
You might also be interested in looking at the Dictionary class if you know ActionScript3. It's basically an Object that doesn't convert its keys to strings, so you can use a key of any datatype. Senocular posted some examples here (http://www.kirupa.com/forum/showpost.php?p=1886034&postcount=36).

Also, AS3 introduces the "is" keyword for datatype checking and it supports custom datatypes.


if (myCustomClass is CustomClass) ? trace("it is!") : trace("it isn't :(");

drexle
08-24-2007, 07:42 PM
That's really neat. I can't say I'm using AS3 right now, unfortunately.

So, in AS3, do Arrays return a typeof "Array?" or are they still "Object?"

Colin Campbell
08-27-2007, 01:52 AM
That's really neat. I can't say I'm using AS3 right now, unfortunately.

So, in AS3, do Arrays return a typeof "Array?" or are they still "Object?"

As far as I know, typeof is only there for backwards compatibility, so my guess is nothing's changed.