ggkmath
ggkmath

Reputation: 4246

another Actionscript newbie error 1118: Implicit coercion of value with static type Object to a possibly

I get this error when compiling an AS3/Flex project:

Error 1118: Implicit coercion of a value with static type Object to a possibly 
unrelated type HRPeople

I've clearly declared dataHR_A to be of class HRPeople, and I've initialized all of the arrays inside the HRPeople.as file. Not sure why I'm getting this error.

My MXML Code looks like (snippet):

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    creationComplete="initApp()"
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[                   
            public var dataHR_A:HRPeople = new HRPeople;

            public function initApp():void
            {               
                //preallocate memory
                dataHR_A.elements = 5;
                dataHR_A.FirstName = new Array(dataHR_A.elements);
                dataHR_A.LastName = new Array(dataHR_A.elements);
                dataHR_A.Email = new Array(dataHR_A.elements);
                dataHR_A.Salary = new Array(dataHR_A.elements);

                dataHR_A = {  // ERROR IS ON THIS LINE OF CODE
                    FirstName:["Donald","Douglas","Jennifer","Michael","Pat"],
                    LastName:["OConnell","Grant","Whalen","Hartstein","Fay"],
                    Email:["OCONNELL","DGRANT","JWHALEN","MHARTSTE","PFAY"],
                    Salary:[2600, 2600, 4400, 13000, 6000]};
            }
and so on ...

Here's the class file for HRPeople.as:

package {
    public class HRPeople {
        public var elements:int;
        public var FirstName:Array = [];
        public var LastName:Array = [];
        public var Email:Array = [];
        public var Salary:Array = [];
    }
}

Upvotes: 0

Views: 1872

Answers (1)

laurent
laurent

Reputation: 90863

You cannot use this kind of syntax in ActionScript 3. Because { ... } is an Object while dataHR_A is HRPeople. To make it work, you need to write it like that:

dataHR_A.elements = 5;
dataHR_A.FirstName = ["Donald","Douglas","Jennifer","Michael","Pat"];
dataHR_A.LastName = ["OConnell","Grant","Whalen","Hartstein","Fay"];
dataHR_A.Email = ["OCONNELL","DGRANT","JWHALEN","MHARTSTE","PFAY"];
dataHR_A.Salary = [2600, 2600, 4400, 13000, 6000]};

Also you can just set the properties directly, you don't need to allocate the memory.

Also, rather than settings an elements property yourself, you could simply create a getter that will dynamically get the number of elements. That way you can add new elements without having to worry about keeping elements current. Something like that would work:

package {
    public class HRPeople {
        public function get elements():int {
            return FirstName.length;
        }
        public var FirstName:Array = [];
        public var LastName:Array = [];
        public var Email:Array = [];
        public var Salary:Array = [];
    }
}

Upvotes: 1

Related Questions