Extending Flex FileReference class to contain another property

1.4k Views Asked by At

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?

3

There are 3 best solutions below

2
On

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. :)

0
On

Extending FileReferenceList is not going to be helpful. FileReferenceList.browse() method creates an array of FileReference object when user selects multiple files - that happens internally (may be in its private methods) and you cannot change that behavior and force it to create SxmFR objects instead. Use custom events as Myk suggested.


This article talks about Sound objects, but may be that's applicable to FileReference objects too. May be you cannot reuse them. Post the code where you use the SmxFr class and get the said error.

2
On

When e.target is instantiate as FileReference you can't cast it to SmxFR because it's not in the line of inheritance. In the other way you can a SmxFR Object to FileRefernce.