7 Replies Latest reply on Sep 11, 2008 12:48 AM by tegnegi

    Class scope

    tegnegi Level 1
      I just cant understand how can we retrieve the private variables of a class through other class. To explain in detail i ve 2 class files Person and Person2. Here is the code inside them.

      Person.as class file

      class Person {
      private var fn:String;
      private var ln:String;
      function Person() {
      fn = "myFirstName";
      ln = "myLastName";
      }
      }



      Person2.as class file

      class Person2 {
      private var root:MovieClip;
      function Person2(mov) {
      root = mov;
      trace(root.pObj.fn);
      }
      }


      The fla file that calls the class has this code on its first frame:

      var pObj:Person=new Person()
      trace(pObj.fn)
      var p2:Person2=new Person2(this) //myFirstName


      My question is how Person2 class can access Person class private variable.



        • 1. Re: Class scope
          kglad Adobe Community Professional & MVP
          it can't. you haven't set it up for that.

          person2 instances have no access to person instances and fn and ln are properties of person instances.
          • 2. Re: Class scope
            tegnegi Level 1
            But if you take the code and try it does happen as i ve mentioned. Moreover pObj is Person2 instance and not Person1 instance, but even then Person2 instance cant have access to its class private variables. Why is it happening here? I ll suggest you to try it.
            • 3. Re: Class scope
              kglad Adobe Community Professional & MVP
              pObj is a Person instance, not a Person2 instance and there is no Person1 class so there aren't any Person1 instances and you didn't mention anything happening.
              • 4. Re: Class scope
                tegnegi Level 1
                M sorry i messed it up a lil bit. Ya you are right taht there isnt ne Person1 class and pObj is an instance of Person class. What i wanted to say/ask was and the thing i didnt mentioned( sorry for that) is when i put the trace code as comment and then run the code it gives the pObj.fn value which is "myFirstName" i.e.

                var p2:Person2=new Person2(this) gives me the "myFirstName". Now my question is how can p2 which is an instance of Person2 class can access the private variable of Person class? If you look at the Person2 class constructor function it traces out the Person instance (pObj) fn (private) property.

                Thanks
                • 5. Re: Class scope
                  tegnegi Level 1
                  M sorry i messed it up a lil bit. Ya you are right that there isnt ne Person1 class and pObj is an instance of Person class. What i wanted to say/ask was and the thing i didnt mentioned( sorry for that) is when i put the trace code as comment and then run the code it gives the pObj.fn value which is "myFirstName" i.e.

                  var p2:Person2=new Person2(this) gives me the "myFirstName". Now my question is how can p2 which is an instance of Person2 class can access the private variable of Person class? If you look at the Person2 class constructor function it traces out the Person instance (pObj) fn (private) property.

                  Thanks
                  • 6. Re: Class scope
                    kglad Adobe Community Professional & MVP
                    if the variable is private, even a person instance can't access it. make the variable public is you want to make it accessible outside the class.
                    • 7. Class scope
                      tegnegi Level 1
                      Yes, you are right, but the problem is that i can access the private variable outside the class i.e. Person2 instance can access the Person class private variables. Why is it happening?

                      The code is

                      var pObj:Person=new Person()
                      //trace(pObj.fn)
                      var p2:Person2=new Person2(this) //myFirstName

                      after the trace code line is commented then the p2( instance of Person2 class) Object can access the private fn property of Person class. Why?? Does one class has access to the private variables of another class? If not, then why is it happening here?