Really annoying ActionScript error
Hi,
I’ve been working with ActionScript for the first time and created a really simple class with a couple of getters and setters and wanted to instantiate the class and use a few setters.
So the original class was:
package MyPackage
{
import mx.core.UIComponent;
public class AClass extends UIComponent
{
public function AClass()
{
super();
}
public function get AValue():Number
{
return _aValue;
}
public function set AValue(_newAValue:Number):void
{
_aValue = _newAValue;
}
private var _aValue:Number;
}
}
That looks fine right?
So in my class I call
import MyPackage.AClass at the top of the file.
Next in another class I called
var myClass:AClass = new AClass(); myClass.AValue(20.0);
I figured that this looked fine, you want to call the function AValue with the value 20.0 passed in. However I was getting the cryptic error:
1195: Attempted access of inaccessible method AValue through a reference with static type MyPackage:AClass
Took me a while to figure this one out I thought I had defined the functions incorrectly or wasn’t importing the class right. However the solution is a simple one. When you use get and set you are essentially saying that you can use the assignment operator but for some weird reason you can’t call the functions directly.
So the solution was to do:
var myClass:AClass = new AClass(); myClass.AValue = 20.0;
Use the assignment operator instead of calling the set function directly.