English 中文(简体)
Extending Flex FileReference class to contain another property
原标题:

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?

问题回答

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

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.

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.





相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

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,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签