6 Replies Latest reply on Jul 18, 2007 1:26 AM by Clark_Kent101

    Accessing public class properties from sub components

    Clark_Kent101
      Hi Everyone,

      I currently have a class which contains one public string property, an empty constructor, and a setUsername method. In this class the setUsername method assigns a string value to that property. I call this setter function when the application initializes and pass it the value i want that string property to be. If i access this public property's value from the component that assigned it a value, i am able to see that value.... however if i try the same thing in a child component after importing this class into that child component, i get a null value.

      So for example:

      In the base of the application i create an instance of the class to access the setter method and pass in a string value to set that public string property...

      -----------------------------------
      //inside the parent component

      import ds.td.ClassName;

      public var _username:String = 'Tony';

      public function init():void
      {
      var obj:ClassName= new ClassName();

      obj.setUsername(_username);

      trace(obj._username);
      }

      If i try trace the _username property in the class I see the correct value.

      -----------------------------------
      -----------------------------------

      Now if i try creating an instance of that class in a child component and try accessing that public string property i get a null value... eg:

      // inside a child component

      import ds.td.ClassName;

      private function init():void
      {
      var obj:ClassName = new ClassName();
      trace(obj._username);
      }

      ------------------------------------
      -----------------------------------

      This is what my class looks like:

      [Bindable]
      public class ClassName
      {
      public var _username:String;

      public function ClassName()
      {

      }

      public function setUsername(username:String):void
      {
      if (username != '')
      {
      this._username = username;
      }
      }

      }

      --------------------------------

      Why do I get a null value when i try accessing that property from a child component after I've assigned it a value from the parent component?

      Thanks for your time,

      - Tony
        • 1. Accessing public class properties from sub components
          levancho Level 3
          obviously you will get null as a result because instance in your child component is different than instance in your parent component.. they have same refference name but they are not same objects.
          • 2. Accessing public class properties from sub components
            Clark_Kent101 Level 1
            Hi levancho,

            Thanks for your response. I know that they're two seperate instances, I just thought that if i create an instance of the class and assign a value to that string property using that instance that i would be able to create another instance in some other component and access what should be there already. So if i populate that public string with something from the parent component, can't i access that value from the child components using another object?

            I tried hard coding a value into that public string in the class, by doing so I'm able to access that value from any component - which is what I'm trying to achieve, what should I do in the parent component to populate that string property in the class so that i can access the value of that property from a child component?

            Thanks for the help.

            - Tony

            -------------------------------
            EDIT:
            -------------------------------

            Would I need to use dispatching of events to share that public string variable in between all my child components? I'm probably trying to share the public string var in the class in an incorrect manner - my first thought was that i could assign a value to that string property in the class and it would remain in there throughout the scope of the application - and then access that property by simply importing that class into the child component, instantiating an instance of that class and then referencing it so i can use that value. That obviously doesn't work so now i'm looking towards dispatching an event to share the data between components. Just not sure how to implement that properly.
            • 3. Re: Accessing public class properties from sub components
              gtb104
              If you want to pass a value from parent to child, you don't need to use an event. There are a couple ways you could go about this, but the simplest way is to do it in the setter of the parent. Assign the _username property to the childs equivalent.

              public function setUsername(username:String):void
              {
              if (username != '')
              {
              this._username = username;
              childName.username = _username
              }
              }

              Your child must have a public property named username, or a private property with matching setter/getter.
              • 4. Accessing public class properties from sub components
                Clark_Kent101 Level 1
                Hi gtb104,

                Thats what i currently have as a workaround, I've been using that method nearly everytime i want to share data between components. Which is the better method though? The one you've suggested, or the dispatching of events so that all components can be notified of the change?
                • 5. Re: Accessing public class properties from sub components
                  ntsiii Level 3
                  It depends on your requirements. Events result in loosly coupled components, making it easier to re-use and maintain them, at the cost of some code complexity and readability.

                  Tracy
                  • 6. Re: Accessing public class properties from sub components
                    Clark_Kent101 Level 1
                    Hi Tracy,

                    I guess dispatching is the way to go then in my case. Do you think I would be correct to conclude that I should probably resort to using the dispatching of events to share data between components when there hierarchy of components becomes complex? And use the other method (childName._globalVar = value) to share data between components when the hierarchy of components isn't that complex/deep? I suppose dispatching of events can be used in both instances, likewise with the other method - I just find it easier to use the other method for a quick and easy solution of sharing data between components.

                    Thanks for the help,

                    - Tony