Skip to main content
September 7, 2011
Answered

Evaluating Switch?

  • September 7, 2011
  • 1 reply
  • 545 views

Just ran across what seems like an odd one, and a cursory Googling didn't get me an answer. Though I'd think I must've run across this before, I couldn't remember...

So why can I evaluate an expression in a switch like so:

switch(vidList[currentIndex].video){
    case "city":
        vid = "assets/smart_city.f4v";
        break;
   ...
}

If I trace vidList[currentIndex].video before the switch I get "city" but tracing vid after the switch I get null

Quickly solved by just assigning to a variable and then using that in the switch... but it still seems odd I can't just do it directly.

var scene:String = vidList[currentIndex].video;
switch(scene){

     ...

This topic has been closed for replies.
Correct answer Kenneth Kawamoto

Switch statement uses strict equaliry (===) to evalutate the expression. Therefore if vidList[currentIndex].video is not a String, it evaluates as false. For example if it's actually an XML node (originates from an XML) or an Object (originates from an Array) you can treat it as a String in normal operations but it won't pass a switch statement if you test it against a String.

Therefore you can do something like this:

switch(vidList[currentIndex].video.toString()){

   case "city":

      ...

1 reply

Kenneth Kawamoto
Community Expert
Kenneth KawamotoCommunity ExpertCorrect answer
Community Expert
September 7, 2011

Switch statement uses strict equaliry (===) to evalutate the expression. Therefore if vidList[currentIndex].video is not a String, it evaluates as false. For example if it's actually an XML node (originates from an XML) or an Object (originates from an Array) you can treat it as a String in normal operations but it won't pass a switch statement if you test it against a String.

Therefore you can do something like this:

switch(vidList[currentIndex].video.toString()){

   case "city":

      ...

September 7, 2011

Ah, damn! Thank you! That was an XML list I was evaluating, so I could've just done toString() on it, or probably casted it to string... Funny the things that trip you up sometimes.

Edit - I read your message... and somehow still decided I could do toString() when you pointed it out directly... yeesh. Been at work for 15 hours so far today. Might be part of it.

Kenneth Kawamoto
Community Expert
Community Expert
September 7, 2011

I learned this the hard way myself too, using switch on XMLList... wasted hours...