1 Reply Latest reply on Jun 3, 2006 7:52 AM by Newsgroup_User

    How to make a *true* read-only property in actionscript2.0

    yavuzakyazici
      Hi everyone,

      I just found out that making a class property private and accessing thru a getter does not guarantee that it is read-only.
      Here is an example...


      try it.
      it works.

      That is a big breach of contract if you ask me.
      How can I guarantee a read-only property in a class?
        • 1. Re: How to make a *true* read-only property in   actionscript2.0
          Level 7
          A property being read-only means that, in your example, you can't modify
          myClass.Test; it does not mean that you can't modify the properties *inside*
          the object returned by myClass.Test.

          Java, C#, and most other languages are the same way. C#:

          class myObject
          {
          public String yavuz;
          }

          class myClass
          {
          private myObject test;
          public myClass()
          {
          test = new myObject();
          test.yavuz= "Yavuz";
          }

          public myObject Test
          {
          get { return test; }
          }

          static void Main(string[] args)
          {
          myClass x = new myClass();
          myObject y = x.Test;
          y.yavuz = "someOtherYavuz";
          }
          }



          --
          Mike Morearty
          Developer, Flex Builder team
          http://www.morearty.com/blog


          yavuzakyazici wrote:
          > Hi everyone,
          >
          > I just found out that making a class property private and accessing thru a
          > getter does not guarantee that it is read-only.
          > Here is an example...
          >
          >
          > try it.
          > it works.
          >
          > That is a big breach of contract if you ask me.
          > How can I guarantee a read-only property in a class?
          >
          > class myClass
          > {
          > private var test bject;
          > public function myClass()
          > {
          > test = new Object();
          > test.yavuz= "Yavuz";
          > }
          > public function get Test () : Object
          > {
          > return test;
          > }
          > }
          >
          > //I can modify the private test property by the Test getter by this simple
          > syntax.
          >
          > x = new myClass();
          > y bject = x.Test;
          > y.yavuz = "someOtherYavuz";
          >