2

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"?

2
  • make sure you call create() BEFORE take() Commented Jan 29, 2012 at 21:49
  • Thanks ToddBFisher, that worked perfectly! Ascension Systems, yes DailyVerse is a class. Commented Jan 29, 2012 at 22:13

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

7 Comments

If DailyVerse is supposed to be a class, you use the same principles just place the getter/setter within the class.
Lazy instantiaion rocks! +1UP. I would also suggest adding the underscore _myRestrictedString but that's just my preference.
It is set up this way because I am horrible at programming and Im not sure of what Im doing :) Im attempting to create a website that pulls a daily verse or quote from an XML file, which I have working. I am trying to take those quotes and put them in a variable so I can then send that to a "favorites" database, and maybe let the user email it. I have the variable working, with the quote from the xml, but I cannot trace it from any other functions. Thanks for all the help for both of you. I am going to give this a shot tonight now that I have a better idea of how it should work.
LOL @Brandon, that's so brutally honest, I just have to up vote your question ;)
lol that is pretty awesome. Don't get discouraged you're doing good! Keep at it you'll improve over time. We're all improving all the time. :)
|

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.