4 Replies Latest reply on Feb 4, 2009 5:54 PM by Newsgroup_User

    public vs private vs protected (readonly properties?)

    Clifford Meece
      Hi,
      Is it possible to have a property in AS3 that is public but read only? A bit like the .x and .y properties of UI components, I would like to define a property in a custom class that can be accessed publicly, but not altered outside the instance.

      thanks,
      Cliff
        • 1. public vs private vs protected (readonly properties?)
          levancho Level 3
          yes, thats what get and set functions are for:

          private var _x:Number;
          public funcion get x() : Number {return this._x;}
          private function set x(newX:number) : void {this._x = newX;}

          now you can only call objectwiththatProperty.x to read it,
          cant set it you will get error saying : propety is read only.
          • 2. Re: public vs private vs protected (readonly properties?)
            Level 7

            "levancho" <webforumsuser@macromedia.com> wrote in message
            news:gmcvca$6n2$1@forums.macromedia.com...
            > yes, thats what get and set functions are for:
            >
            > private var _x:Number;
            > public funcion get x () : Number {return this._x;}
            > private function set x (newX:number) : void {this._x = newX;}
            >
            > now you can only call objectwiththatProperty.x to read it,
            > cant set it you will get error saying : propety is read only.

            Note that the example above includes both a getter and a setter, so you will
            be able to set the value just fine without getting an error. If you want to
            have a read-only property, then omit the getter.

            Also note that if you want to use data binding to bind to the property,
            you'll need to specify a custom event and dispatch that event in whatever
            part of your code sets the internal variable (or whenever you want the
            program to think the variable has changed, whichever seems more
            appropriate).

            HTH;

            Amy


            • 3. public vs private vs protected (readonly properties?)
              levancho Level 3
              quote:


              Note that the example above includes both a getter and a setter, so you will
              be able to set the value just fine without getting an error. If you want to
              have a read-only property, then omit the getter.


              hate to be a party crasher but, its not correct, setter has private modifier so it can only be accessible from within that class, and from withing that class you,may as well call _x ,...
              so setter is not visible,
              and can not be set outside of that class, that makes that x Read only :)
              Regards
              levan.
              • 4. Re: public vs private vs protected (readonly properties?)
                Level 7

                "levancho" <webforumsuser@macromedia.com> wrote in message
                news:gmdfpm$phm$1@forums.macromedia.com...
                >
                quote:


                > Note that the example above includes both a getter and a setter, so you
                > will
                > be able to set the value just fine without getting an error. If you want
                > to
                > have a read-only property, then omit the getter.
                >

                > hate to be a party crasher but, its not correct, setter has private
                > modifier
                > so it can only be accessible from within that class, and from withing that
                > class you can may as well call _x , so setter is not visible,
                > and can not be set outside of that class, that makes that x Read only :)

                You're right, I didn't notice that, since when I've tried that syntax it
                threw an error, so I never suspected someone would suggest that syntax to a
                new user :->