Some notes on migrating applications from Flex 1.5 (AS2) to Flex 2 (AS3)

I'm going to use this post to document issues I've found while migrating my Flex 1.5 (ActionScript 2) applications to Flex 2 (ActionScript 3). I've decided to include the error message you will receive for a given issue as the title, along with the reason and a solution to the issue. This is not meant to be an exhaustive list and I'll keep updating it as I find time and uncover new ones.

Call to a possibly undefined method init through a reference with static type mx.containers:Form.

Reason: When using code-behind in Flex 1.5, you would override the init() event handler and listen for the "childrenCreated" event. (This was the Flex 1.5 equivalent of using the onLoad() event handler in Flash.) With Flex 2, the init() method no longer exists.

Here's an example of the old ActionScript 2/Flex 1.5 code:

function init ():void
{
  super.init();
  addEventListener ( "childrenCreated", onLoad );
}

Solution: In Flex 2, your should override the initialize() event handler and listen for the FlexEvent.CREATION_COMPLETE event. The Flex 2 equivalent is shown below:

override public function initialize():void
{
  super.initialize();
  addEventListener ( FlexEvent.CREATION_COMPLETE, creationCompleteHandler );
}

private function creationCompleteHandler ( event:FlexEvent ):void
{
  onLoad();
}

Implicit coercion of a value with static type Object to a possibly unrelated type flash.events:Event.

Reason: You are defining an event as a simple Object instance -- eg. dispatchEvent ( { type: "orderProcessed" } );

Solution: Create an instance (or subclass) of the mx.events.Event class instead -- eg. dispatchEvent ( new Event ( "orderProcessed" ) );

Access of possibly undefined property length through a reference with static type mx.controls:List. (or ComboBox, DataGrid, etc.)

Reason: In Flex 1.5, components that took data providers would proxy data provider method calls to the data provider object. This meant that you could reference methods of a component's data provider directly through the component -- eg. myListBox.getItemAt(0);. This is no longer the case.

Solution: Reference the data provider of a component through the dataProvider property. eg. myListBox.dataProvider.getItemAt(0);

A file found in a source-path must have an externally visible definition. If a definition in the file is meant to be externally visible, please put the definition in a package.

and

Unable to locate specified base class 'org.osflash.arp.samples.pizza.flex2.view.OrderFormClass' for component class 'OrderForm'

Reason: You have not updated your ActionScript class to use the new package keyword. In ActionScript 3, you define a classes' package separately, not in the class signature.

Solution: Use the package keyword to define the package for your class -- package org.osflash.arp.samples.pizza.flex2.view { public class OrderForm { //... } }.

Attempting to initialize non-public inherited property 'ordersLb' from MXML.

Reason: You have declared components inherited from an MXML file as private properties. In order for code-behind to work in Flex 2, you have to declare components as public in your classes. This is a current limitation in the code-behind feature in Flex 2 and will hopefully be improved in future releases (I'm still holding my breath on an implementation of partial classes in a future revision of ActionScript 3.)

Solution: Declare components inherited from MXML documents as public in your classes.

Type was not found or was not a compile-time constant: Void.

Reason: In ActionScript the Void datatype has been replaced with the void datatype (lowercase "v")

Solution: Replace "Void" with "void"

A constructor can only be declared public.

Reason: In ActionScript 3, you cannot have a private constructor. This will affect you when migrating Singletons to AS3.

Solution: Make the constructor public. You can add some logic to your class to throw an error if it detects multiple intances of a Singleton class being created but, unfortunately, due to this limitation, you cannot have a true Singleton in AS3.

(Run-time error) ArgumentError: Error #1063: Argument count mismatch on org.osflash.arp.samples.pizza.flex2.view::OrderFormClass/::validateForm(). Expected 0, got 1.

Reason: Your event handler in ActionScript 2 did not take any arguments. In AS2, you could get away with creating event handlers that ignored the event object that was sent to them. You cannot do this in AS3 however, as it results in a run-time error.

Solution: Make sure your event handlers define an event parameter of type Event (or subclass of Event.)

(Warning) return value for function 'functionName' has no type declaration.

Reason: You will see this warning if you were not in the habit of explicitly declaring your public methods by using the public access modifier. In ActionScript 2, methods defaulted to being public. In ActionScript 3, they default to the new access modifier "internal".

Solution: Get into the habit of using access modifiers on all your method signatures -- eg. public function myFunction ():void {};

Comments