BrandonG
BrandonG

Reputation: 915

Having a issue casting a object from an ArrayCollection to an instance of my Custom Class

I am using Flex 4 and for whatever reason I cannot get the following code to work which happens inside of a ListEvent handler for a dataGrid:

_tempRule = DataGrid(event.currentTarget).selectedItem as Rule;

Rule is a custom class, and the above code always returns null. The dataprovider for the datagrid is an ArrayCollection. If I try to wrap the above code to make it like the following:

DataGrid(event.currentTarget).selectedItem as Rule

I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@e15a971 to com.mycompany.arcc.business.Rule

Now I know I have done this before with native Flex classes like Button, etc, but it my case it will not work.

Here is the Rule class:

    package com.mycompaany.arcc.business
{
    import flash.utils.describeType;

    import mx.collections.ArrayCollection;

    [Bindable]
    public class Rule extends Object
    {
        public static const RANGE:String = "Range";
        public static const SINGLE:String = "Single";
        public static const LIST:String = "List";

        /*name of the rule*/
        private var _name:String;

        /*rule group, like a radio group, only 1 rule from a group can be selected*/
        private var _group:String;

        /*Description of the group for the rule*/
        private var _groupDescription:String;

        /*Description of the rule*/
        private var _description:String;

        /*arry of values for this rule, based on the control type*/
        private var _values:ArrayCollection;

        /*min num of values*/
        private var _numValues:int;

        /*type of control to build, if range, 2 inputs, single, 1 , list 1 or more*/
        private var _type:String;

        public function Rule(name:String=null, group:String=null, description:String=null, values:ArrayCollection=null, numValues:int=0, type:String=null)
        {
            super();
            _values = new ArrayCollection();

            this._name = name
            this._group = group;  
            this._description = description;
            if (values)
            {
                this._values = values;
            }
            this._numValues = numValues;  
            this._type = type;  
        }


    }
}

So what am I missing?

Upvotes: 3

Views: 2662

Answers (2)

BrandonG
BrandonG

Reputation: 915

The quick and easy solution was to add the [RemoteClass] metatag at the top my custom class. I found this solution on Adobe's website, livedocs.adobe.com/flex/3/html/…. It seems that using the native drag and drop capabilities between list-based components causes custom classes to lose their type during copying. Thanks to everyone for the assistance.

Upvotes: 2

Frank
Frank

Reputation: 800

the errormessage is the result of an failed casting. The better way is to use the cast like this,

Rule((event.currentTarget).selectedItem);

In this case, you will receive an exception, when the cast fails and not a null reference BR Frank

Upvotes: 1

Related Questions