I have a problem whereby in AS3 using Flash Builder 4 using a public getter with an internal setter gives me the error "property is read only", like so:
public function get aVar():int{ return _aVar; }
internal function set aVar(value:int):void { this._aVar = value; }
I have used a workaround of:
public function get aVar():int{ return _aVar; }
internal function setAVar(value:int):void { this._aVar = value; }
This seems to be a bug in AS3, or maybe I am missing something? Does anyone know of a better workaround?
Thanks
Getter and Setter must have the same access type, that's not a bug.
As far as I know the getter and setter must be identical, so either both public or both internal.
I'm currently having trouble finding the docs to back this up, although I have had similar error in the past when trying to mix public and private.
The issue has come up before and I have already mentioned the workaround in my blog. The problem lies with how binding works in Flex since it needs to compare values before a binding event is dispatched (by default). The workaround is easy enough however, but slightly annoying because of the extra code:
// Internal Variable
private var __state:String = "someState";
// Bindable read-only property
[Bindable(event="stateChanged")]
public function get state():String
{
return this._state;
}
// Internal Getter-Setter
protected function get _state():String
{
return this._state;
}
protected function set _state(value:String):void
{
this.__state = value;
dispatchEvent(new Event("stateChanged"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With