Search code examples
actionscript-3actionscriptinterfaceinstanceof

applying the is operand or the instanceof in a case statement


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


Solution

  • 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) ...
     }