0

How can I access an variable from another function?

I have a function that sets and variable:

private function create () {
  var str:String = "hello";
}


private function take() {
  var message:String = str;
}

1 Answer 1

4

You didn't specify whether or not the functions are in the same class or in different classes, but your main problem is variable scope. The str variable is defined inside the create function and therefore it's bound in the function's scope. You'll have to declare the variable in a larger scope. If the functions are in the same class, try something along these lines:

private var str:String;

private function create () {
  str = "hello";
}


private function take() {
  var message:String = str;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.