Get the type in flex

2019-02-08 07:18发布

问题:

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

回答1:

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.



回答2:

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;
   }


回答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']);
    }
}


回答4:

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().



回答5:

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);
}


回答6:

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!



回答7:

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;

    }
}