Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal setter with public getter doesn't work

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

like image 610
Christopher Grigg Avatar asked Jul 13 '11 09:07

Christopher Grigg


3 Answers

Getter and Setter must have the same access type, that's not a bug.

like image 182
Timofei Davydik Avatar answered Sep 22 '22 20:09

Timofei Davydik


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.

like image 20
shanethehat Avatar answered Sep 25 '22 20:09

shanethehat


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"));
}
like image 21
J_A_X Avatar answered Sep 21 '22 20:09

J_A_X