Suppose I’m creating a class called Hitchhiker.
package hi.my.name.is {
public function Hitchhiker():void {
// ...
}
public function nullify():void {
this = null;
}
}
And then it gives me an error:
Error: Cannot assign to a non-reference value.
What the heck? I though you can set an instance to null, like:
var hitchhiker:Hitchhiker = new Hitchhiker();
hitchhiker = null;
Why can’t I do this in the class? I’m intending to remove all event listeners by setting the reference to weak reference, then nullify the instance to remove them all. Why can’t I?
Anybody help me?
Where have you defined the class in the first place? This is how you define the class:
Besides your error comes here:
this = null;Basically the error means you cannot nullify the object from inside the instance of the class, you can only nullify the object from outside, which you are already doing.
So remove the function ‘nullify’.
Example of using dispatchEvent:
You can listen to it by:
Or a better way could be to use Arrays to keep track of the objects created.
But I am still not sure why you don’t want GC to handle this.