Scrimmers
Scrimmers

Reputation: 458

Flex 3.2 casting problem

I'm newish to flex an have recently hit a snag casting.

this is the code I'm running.

/**
* return background definition depending on time
*/ 
private function findBackgroundItem( relativeTime : Number ) : CSBackgroundItem
{
    if( this.backgroundItems == null ) return null;
    var result :CSBackgroundItem = null;
    var relative:Date = new Date(relativeTime);
    for( var i : Number = 0; i < this.backgroundItems.length; i++ )
    {
        // backgroundItems is an Ilist of CSBackgroundItem.
        var colourItem : CSBackgroundItem = CSBackgroundItem( this.backgroundItems.getItemAt( i ) );

        // other stuff here
    }           
    return result;
}

The problem occurs when the IList.getItemsAt() result is cast to the CSBackgroundItem variable colourItem. The following error is thrown

TypeError: Error #1034: Type Coercion failed: cannot convert com.mystuff::CSBackgroundItem@650e8dd1 to com.mystuff.CSBackgroundItem.

If I use the 'as' keyword I get cast results in the colourItem being null. Using the debugger shows that the list is not empty and is indeed populated with CSBackgroundItem objects.

Now this is the wacky bit.. this code works, the first time the module it's in loads.. subsequent loads (after unloading it) throw the exception.

Can anyone shed any light on why this might happen?

Upvotes: 3

Views: 1136

Answers (3)

Richard Szalay
Richard Szalay

Reputation: 84734

FYI, a type loaded into a child ApplicationDomain can be cast to a type (that it extends/implements) in the parent ApplicationDomain.

Eg.

loader.applicationDomain = ApplicationDomain.currentDomain; // parent domain
loader.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); // child domain
loader.applicationDomain = new ApplicationDomain(); // new domain

Upvotes: 1

Scrimmers
Scrimmers

Reputation: 458

Yup.. that works..

here is the fix i used in MXML style..

<mx:ModuleLoader id="loader" x="10" y="10" width="100%" height="100%" applicationDomain="{ApplicationDomain.currentDomain}"/>

the equivilant actionscript would be

loader.applicationDomain = ApplicationDomain.currentDomain;

Upvotes: 1

Scrimmers
Scrimmers

Reputation: 458

I think I may have found the answer..

My guess is the module is loading in a different ApplicationDomain.. which would mean you can't cast from the same type in the current domain..

As I understand this is vaguely similart to different Java ClassLoaders loading the same class.

I'll post again once I've confirmed it

Upvotes: 1

Related Questions