2

i have many objects of the same custom class, and another many objects of another custom class. i would like to create a switch statement to determine from which of the classes the object belongs. the following code doesn't compile, so i'm not sure if this is possible. is the only alternative to use if statements?

function mouseClickEventHandler(evt:MouseEvent):void
     {
     switch (evt.currentTarget)
            {
            case (is customClassA):  trace("is instance of customClassA");  break
            case (is customClassB):  trace("is instance of customClassB");
            }
     }

2 Answers 2

12

This should work:

function mouseClickEventHandler ( evt:MouseEvent ):void
{
    switch ( evt.currentTarget.constructor )
    {
        case CustomClassA:
            trace("is instance of customClassA");
            break;

        case CustomClassB:
            trace("is instance of customClassB");
            break;
    }
}

See Object.constructor.

Sign up to request clarification or add additional context in comments.

Comments

3
function clickHandler (event:MouseEvent):void
{
    var target:Object = event.currentTarget;
    switch (true)
    {
        case (target is CustomClassA):
            trace("is instance of customClassA");
            break;

        case (target is CustomClassB):
            trace("is instance of customClassB");
            break;
    }
}

Not sure if braces are needed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.