views:

253

answers:

1

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;
}
+4  A: 

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;
}
kkyy
Thanks, exactly what I was looking for.
mofle