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 = [];
}
}
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:Also you can just set the properties directly, you don’t need to allocate the memory.
Also, rather than settings an
elementsproperty yourself, you could simply create agetterthat will dynamically get the number of elements. That way you can add new elements without having to worry about keepingelementscurrent. Something like that would work: