How to get datatype?


I’m sure this is a dumb question: is there a method to return the datatype of a variable?

Sheeeshhh, thanks!

simply do a trace on your variable like this:

that will return a number of things, like “movieclip” for example. But some items, in as2, like a class instance for example, will (I think) only return “Object”.

What data type are you looking for? That may be a better way to handle your issue - not sure. For example, you can trace(isNaN(Number(yourVariable))); If that returns true, then your variable is not a number - as in your variable is Not A Number.

in as3 you can use the “is” operator - which handles this issue much easier.

Man, thanks creatify, that’s some great info!

Basically, I was studying some code that parsed xml, trying to get my head around the concepts. In the code, xml nodes and or firstChild elements were being assigned to variables, but those variables weren’t being declared with their datatype (strong typing, etc)…so I was unclear as to whether those variables were of type Array or XMLNode, etc, and wanted to check the variable type to see how the xml data was being handled, etc.

aha - k, yeah, I believe all values coming from the xml are going to come through as strings - so you have to cast them as the datatypes you’d like. I don’t know the best way to cast an Array, for example in as2. Usually, if I have items that need to come through as an array, I’ll just use a delimeter, like a comma or an asterisk between the items in the xml. Then in Flash, I’ll split the string on that delimeter to create the array.

if you have an item in xml, for example, that is to be a number, lets say an attribute:

<yournode id=“5”>asdfasdf</yournode>

if you try adding, in flash, that id to something, say the number 10 - you’ll get “105” as a string, but if you isolate that item from xml, you can cast as a number:

var num:Number = 10+Number(id); // you’ll get 15