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

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Brandon
  • 81
  • 3
  • 10

1 Answers1

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"
  • If DailyVerse is supposed to be a class, you use the same principles just place the getter/setter within the class. –  Jan 29 '12 at 21:52
  • 1
    Lazy instantiaion rocks! +1UP. I would also suggest adding the underscore `_myRestrictedString` but that's just my preference. – ToddBFisher Jan 29 '12 at 21:54
  • 2
    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. – Brandon Jan 29 '12 at 22:18
  • LOL @Brandon, that's so brutally honest, I just _have_ to up vote your question ;) – weltraumpirat Jan 29 '12 at 23:13
  • 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. :) –  Jan 29 '12 at 23:25
  • Ascension Systems, I tried your example but I am getting an error- "Access of possibly undefined property DailyVerse through a reference with static type class" I think I have this whole thing set up wrong or something. Im pulling an xml entry and assigning it to a variable "theQuote". I put that variable into a textfield which displays perfectly. Next Im trying to take that same variable and insert it into a database of "favorites". I cannot even trace it from my insert function. Would it be better to just read what is in the text field instead of using the variable? – Brandon Jan 30 '12 at 02:31
  • @Brandon seems like there's more of an issue here than just the variable access... if you want you could contact me. It's my username on here @ gmail.com I can maybe take a quick look at what you've got instead of taking guesses at questions/answers on here. –  Jan 30 '12 at 20:33