private setter, public getter

It is possible to make private setter and public getter on actionscript classes. All you need is to use proper namespaces for getter and setter within the class.

package
{
    public class GSTest extends Object
    {
        private var _foo:String;

        public function GSTest()
        {
            super();

            // setting value
            private::foo = "bar";

            // getting value
            trace(public::foo);
        }

        public function get foo():String
        {
             return _foo;
        }

        private function set foo(value:String):void
        {
             _foo = value;
        }
    }
}

usage:

var gsTest:GSTest = new GSTest();
trace(gsTest.foo); // returns "bar"

compilation error:

var gsTest:GSTest = new GSTest();
gsTest.foo = "hallo";

Leave a comment

Please be polite and on topic. Your e-mail will never be published.