R – Passing along information on a FileReference Complete event

actionscript-3apache-flexevents

I need to pass along a string with my FileReference, or provide that string as an argument when an event fires. To be clear, it really annoys me that AS3 doesn't allow you to pass parameters on events.

Right now, I've extended the FileReference class to include an additional variable. I'm trying to get this to compile, but it won't compile; I think I don't know how to import this class correctly. If you can tell me how to import this class correctly so that I no longer get Error: Type was not found or was not a compile-time constant at compile time that would be great.

This is the extended FileReference class:

import flash.net.FileReference;

public class SxmFR extends FileReference {

  public var housenum:String = "";

  public function SxmFR(str:String) {
      housenum = str;
      super();
  }
}

I've tried that in a .mxml and a .as in the same folder. Neither is automatically imported.

I've also tried to extend the Event class, but I couldn't figure out how to make the event dispatch, since I need to respond to the Event.COMPLETE event. If you can tell me how to make it dispatch on this, it also might work.

Please help me get this figured out, and much love and thanks to all involved. : )

Best Answer

If you add your event listener as a closure you have access to the variables in the current function:

function myFunction(): void {
    var aParam: String = "This is a parameter";

    dispatcher.addEventListener("eventName", function (e: Event): void {
        // you can access aParam here
        trace(aParam);
    });
}

The aParam inside the function will have the same value as it had when addEventListener was called.