15

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

Sebastian Müller
  • 5,471
  • 13
  • 51
  • 79

7 Answers7

18

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
  • 4,468
  • 18
  • 19
4

ITS WORKING :)

Following is the way I solved this issue

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;
   }
silwar
  • 6,470
  • 3
  • 46
  • 66
Adi Cohen
  • 1
  • 1
3

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,446
  • 1
  • 22
  • 27
3

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']);
    }
}
davr
  • 18,877
  • 17
  • 76
  • 99
Adrian Pirvulescu
  • 4,308
  • 3
  • 30
  • 47
2

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
  • 10,574
  • 13
  • 75
  • 116
  • You can use Object instead of * unless you are expecting namespaces and other crazy items in your array. – Richard Szalay Jun 25 '09 at 14:56
  • 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 Jun 26 '09 at 14:39
1

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
  • 10,276
  • 2
  • 35
  • 45
0

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;

    }
}
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166