Brandon
Brandon

Reputation: 81

Flash cs5: Set a variable using a function, then read that variable from a differect function

I have a public variable and I am trying to set it, then read it from a different function:

public var str:String;

public function DailyVerse() 
{
    function create() {
        str = "hello";
    } 

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

My trace results says null. Why does it not give me "hello"?

Upvotes: 2

Views: 726

Answers (1)

user562566
user562566

Reputation:

I'm not sure why you have this set up this way.... if you want to get and set variable, you use the getter and setter syntax for flash.

private var myRestrictedString:String;

public function get DailyVerse():String {
   if(myRestrictedString == undefined) {
      //Not yet created
      myRestrictedString = "Something";
   }
   return myRestrictedString;
}

public function set DaileyVerse(string:String):void {
   myRestrictedString = string;
}

Now you can access this from outside of your class like so:

myClass.DailyVerse = "Test";
trace(myClass.DailyVerse); //Outputs "Test"

Upvotes: 2

Related Questions