AS Switch/case problem
- Started
- Last post
- 7 Responses
- monNom
i've got a switch/case set up where the cases check for a keyword and set a variable accordingly.
I can trace the variable from within the case, but outside of it it shows up undefined... I've tried setting the variables to the _root, to 'this', and under a container object, nothing is working...
how do I get my variables out of the switch?
- enjine0
do not declar them with the var keyword within the body of the switch statement.
the length of their existence is only during the duration of the switch evaluation.
so just say
var myVar;
and then begin your switch statement, and just refer to myVar (without the var in front of it)
- monNom0
hmm, doesn't seem to be working... Maybe I'm not implementing it correctly.
here the code: (original and my probably wrong implementation of your tip.)
- monNom0
anyone?
- unformatted0
where do you actually call your function?
- unfittoprint0
enjines's right.
//just add before the switch statement:
var node:String = holder.childNodes[i].nodeName
//then
switch(node){
etc......
}
- monNom0
got it. Thanks for the help.
Turns out it was nothing to do with scope. I was completing the script before the data had loaded, so the variables were updated after the script had finished.
//great way to waste the day.
- enjine0
just got back in and saw this.
yeah, making sure the data is loaded before you parse it helps ;)but also, just wanted to note that if you did do that myVar thing, as you did in the second version, in order to stick properties on it on the fly as you did, you'd have to first instantiate myVar as a new object--
var myVar = new Object();
otherwise, those property/value pairs won't get added anyway.
glad you got it fixed though.
word out.