This content has been marked as final. Show 6 replies
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.
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.
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.
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.
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?
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.
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,