2

I am using addProperty() to dynamically populate a Class with getters. Here is a snippet of my code that does just that:

for (var i in options)
{
    this.addProperty(i, getOption, null);
    this._optionCount++;
}

//a little while later....

public function getOption() 
{
    return "hellO";
}

This is working successfully because when I call the following from the timeline (Thie previous code is part of a class called DropDown):

var options:Object = new Object();
options.one = "hello";
options.two = "goodday";
options.three = "yo";
options.four= "MEGA AMAZING";
options.five= "yo yo yo";
var drop1:DropDown = new DropDown(this.drop1, "red", options)

And attempt to trace out all of the properties in var drop1 by:

for (var i in drop1) 
    trace(i+" = "+drop1[i]);

I get the following:

_initY = -107
_mask = _level0.drop1.mask
_dropdown = _level0.drop1.options
_optionWidth = 143
_button = _level0.drop1
one = hellO
two = hellO
three = hellO
four = hellO
_optionCount = 5
five = hellO
_shadow = 8330268
_highlight = 9249057

Yet when I attempt to trace the following:

trace(drop1.one);

It gives me a compiler error saying:

There is no property with the name 'one'.

Does anyone know why I can't access this property?

George Reith
  • 13,132
  • 18
  • 79
  • 148

2 Answers2

0

You want to look into AsSetPropertyFlags and how to set properties enumerable. However, I wouldn't do it the way you are doing, instead, I would have setOption(option) and getOption(option) functions - less code, less indirections, less obscurity. Worst case, I'd implement __resolve() in my class.

As an aside, if you are writing AS2 in classes, then it probably would make sense to look at a better compiler (MTASC or FLASC), then that you seem to be using.

  • Quite sure __resolve() won't help the compiler? (But again, it's a long time since I've looked as AS2). But agree on dropping addProperty(). – JimmiTh Feb 01 '12 at 18:51
  • Agree completely, but the error message "There is no property with the name 'xxx'" is exactly that: The compiler's prevention of access to unknown properties (as opposed to a runtime error). – JimmiTh Feb 02 '12 at 12:33
0

Just a guess - been a long time since I've used AS2.

The moment you declare a type in AS2, the compiler thinks it knows all about that type. It looks at the class declaration and takes that as gospel. Which means, it won't allow you to assign to properties that aren't in the class declaration.

As far as I recall, you're not the only one to fall into that AS2 trap. There have been a few classes that had methods or properties missing from the intrinsic class declarations that were shipped with each version of Flash. Meaning that you couldn't access them in a type safe way.

There's no really good solution, but there are workarounds, although they're all about removing the type safety you've tried to enforce. In order of (my) preference, from worse to better:

  • Don't declare a type: var drop1 = new DropDown(this.drop1, "red", options); should work.
  • Declare your class as dynamic: dynamic public class DropDown() ... (which of course also gets rid of the type safety)
  • Use array access: trace(drop1["one"]); (preferred to the previous ones because it keeps the advantages of type safety for the rest of the class).
  • Don't bother with addProperty. Use a function style setter/getter instead (getOption() / setOption()).

Reason for preferring the last one: It may not "look as nice" (if addProperty actually worked well), but it works, and it's easy to decipher what's happening in the code. Unlike addProperty, which adds a level of indirection that is more likely to make your added properties look like magic to someone else reading your code.

JimmiTh
  • 7,389
  • 3
  • 34
  • 50