views:

208

answers:

1

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

+2  A: 

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.

Markus Johnsson
Hmm... this almost works. I am using a for loop to iterate over this, but it seems to use the same object initially created in the first pass of the loop, because it always tries to apply the first value of `aParam` to subsequent events assigned by the same loop. Do you know how to fix that?
cookiecaper
If you loop over an array `a` you can use `a.forEach` instead. That will create a new variable for each iteration of the loop. See my answer on this question for example:http://stackoverflow.com/questions/1715918/in-actionscript-3-how-can-i-pass-the-current-value-of-an-array-in-a-loop-to-an-e/1718024#1718024
Markus Johnsson