views:

92

answers:

2

I am trying to figure out how to apply the is operand or the instanceof in a case statement to determine which datatype a interface object belongs too. keep getting errors

switch (IOjbect is)
            {
            case Tile:
            trace ("Load Tile");
            break;
            case House:
            trace ("A House Load");
            break;
            default:
            trace ("Neither a or b was selected")
            }

anyone have any ideas

+1  A: 

You can't use a is as you are trying todo in switch/case:

Use an If instead:

var myObject:IObject=...
if (myObject is Tile){
 var myTile:Tile=Tile(myObject); 
 // you can cast myObject to Tile since the IS return true
 // otherwise it will raise an exception 
} else if (myObject is House){
 var myHouse:House=House(myObject);
}

For the as it will return null if it is not of the type you want:

var myObject:IObject=...
var myHouse:House=myObject as House;
 if (myHouse===null){
  var myTile:Tile=myObject as Tile;
  if (myTile===null) ...
 }
Patrick
+1  A: 

Unfortunately, the switch case statement doesn't work like that. You must only put simple objects in the switch statement (not code).

For what you're trying to accomplish, I would go with if/else statements.

Gabriel McAdams