views:

2598

answers:

8

Hello community,

can someone tell me how I can identify the type of an object in flex? In particular I have an array where I store multiple types in (but all UIComponents) now as I evaluate the array I want to find out whether I have a TextInput Control or a RadioButton. Does someone have an idea?

Thanks in advance

+6  A: 

You can either test against a particular class using the "is" operator or you can use flash.utils.getQualifiedClassName() (you pass it your object) - which will return a string of the fully qualified class name.

Branden Hall
Ah yes -- getQualifiedClassName(). It's come in handy more than a few times.
Christian Nunciato
+1  A: 

The is operator tests for type compatibility, yes. From the docs, is:

... evaluates whether an object is compatible with a specific data type, class, or interface. Use the is operator instead of the instanceof operator for type comparisons. You can also use the is operator to check whether an object implements an interface.

Other useful operators in this category are typeof (which returns a string representation of a primitive), instanceof (similar to is, but disregards interface compatibility) and as. A great and complete list of ActionScript operators is available here.

Hope it helps!

Christian Nunciato
+3  A: 

The operator "is" represents one option.

Then there is the operator instanceof, which might or might not be useful depending on situation.

Also there's the ObjectUtil class with static method getClassInfo. This one returns more than just the object's class name.

Operator "typeof" unfortunately is useless for classes.

And, as Branden Hall already suggested, flash.utils.getQualifiedClassName().

bug-a-lot
+2  A: 

here's some simple pseudo-code which demonstrates how to use the is operator for what you want to do:

for each (var item:* in myArray)
{
    if (item is TextInput)
        doSomethingWithTextInput(item as TextInput);
    else if (item is RadioButton)
        doSomethingWithRadioButton(item as RadioButton);
}
Eric Belair
You can use Object instead of * unless you are expecting namespaces and other crazy items in your array.
Richard Szalay
Good point, I was just trying to make it as flexible as possible. In fact, if you know you're only going to have UIComponents or DisplayObjects, then you could use that.
Eric Belair
+1  A: 

Your best bet is using the "is" operator and use something like:

for( var i:int = 0; i < componentArr.length; i++ )
{
    var comp:UIComponent = componentArr[ i ];
    if( comp is DataGrid )
        // Handle DataGrid functionality here.
    else if (comp is DropDown )
        // Handle DropDown here
}

There is one problem with this approach, however. Because "is" will return true for all descendant classes, you must put all of the descendant classes before their ancestors -- List must come before ListBase. This can cause some annoyances.

// This is important to remember:
var mc:MovieClip = new MovieClip();
trace( mc is Sprite ); // true

There is one other option for cases where you want objects to be a member of a specific class (and not a descendant class): you can use the constructor property of the object and use a switch statement.

for( var i:int = 0; i < componentArr.length; i++ )
{
    var klass:Class = componentArr[ i ].constructor;

    switch( klass )
    {
        case DataGrid:
            // Handle DataGrid
            break;
        case Text:
            // Handle Text
            break;
        case NumericStepper:
             // Handle NumericStepper
            break;
        default:
             // Handle default
            break;

    }
}
Christopher W. Allen-Poole
+3  A: 

Try to use the "className" property.

It should return "TextInput" or "Button" depending the case

for each (var item:* in myArray)
{
    if(item.hasProperty('className'))
    {
        trace("item ["+i+"] is :" + item['className']);
    }
}
Adrian Pirvulescu
A: 

ObjectUtil.getClassInfo(

cloverink
A: 

ITS WORKING :))))

switch( true )
   {
    case item is Customer_SF:
     c = item as Customer_SF;
     break;

    case item is Opportunity:
     o = item as Opportunity; 
     break;

    case item is Product:
     o = ( item as Product )._opportunity;
     break;
    default:
     return true;
   }
Adi Cohen