R – Extending Flex FileReference class to contain another property

actionscript-3apache-flexextending-classesfilereferenceinheritance

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn't let me pass arguments to functions through event listeners, which makes me feel sad, so I need this property to exist on the event target, so I can access it.

I also want to be able to cast extant FileReference objects to this class without any fuss. I have:

var fr:SmxFR = e.target as SmxFR

and I want that to work; right now it just returns null.

A blank, newly instantiated SmxFR object has the extended property in place, but all of its inherited properties and objects return Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.

This is the class I am using, SmxFR.as:

package
{
import flash.net.FileReference;

public class SmxFR extends FileReference
{
    public var housenum:String = "";
    public function SmxFR()
    {
        super();
    }       
}
}

Kept it as straightforward as I could, really. Can someone please help me figure this out? Thanks.


Edit:

Per request, this is the instantiation which results in the aforementioned error in all inherited objects:

var fr:SmxFR = new SmxFR();

I get living handle property from that, and all other (that is, inherited) properties throw Error #2037.


So, maybe what I want to do is going to require overriding FileReferenceList? If the original objects must be instantiated to SxmFR, that's what I'll have to do, since I'm using FRL to allow the user to select multiple files at once. Are you guys sure there is no way to fast from a FileReference to my class?

Best Solution

You can totally pass objects via event listeners, it's just done in a specific way. I'd learn to do it correctly, rather than trying to extend a core library which could cause you problems later if you make a small mistake.

My solution: instead of extending FileReference, extend Event and add your properties to that.

var myEvent:MyExtendedEvent = new MyExtendedEvent();
myEvent.myCustomProperty = myValue;
dispatchEvent(myEvent);

Then in your handler you just write:

function myEventHandler(e:MyExtendedEvent):void {
     trace(e.myCustomProperty);
}

Much more painless to go down this road! The added benefit is that if any other Flash Developer anywhere ever looks at your code they're not going to get hit in the face with a non-standard customized FileReference. :)

Related Question