Nov 23, 2009

Note: How To Retrieve Variables From Function To Another

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > ActionScript

Note: How To Retrieve Variables From Function To Another

demolaynyc
I have been having troubles with this but I found out a solution to this. The problem is to declare variables inside a function and retrieve its value from another function--but that doesn't work.

Here is an example of the wrong thing to do:

CODE

function firstFunc () {
     var aVariable = oneValue;
}

function getVariable () {
     trace (aVariable);
}


The trace would return (undefined) because it is undefined in that exact function.

Solution:
do not declare variables inside a function. If you have to, use _root.variableName instead of var variable Name

Here's a sample code:

CODE

function firstFunc () {
     _root.aVariable = oneValue;
}

function getVariable() {
     trace(_root.aVariable);
}


So, tell me what you think. And if there's a better alternative please do post it.

 

 

 


Comment/Reply (w/o sign-up)

seec77
I'm not sure, but I think you can also do something like this:
CODE

var aVariable;

function firstFunc() {
     aVariable = oneValue;
}

function getVariable() {
     trace(aVariable);
}

I don't have Flash right now to test this, but I'm guessing it should work (it works in plain ol' Javascript, so I don't forsee a problem - it's nothing special to Flash, merely programming semantics). Setting the variable as a member of "_root" is OK, but not necessarily that "correct" programatically, and I think that this method is better.

Comment/Reply (w/o sign-up)

demolaynyc
LOL. Wow, looking back to this it just shows how much of a novice I was at AS and this was pretty recent. Yep, your code should work. The best method is yours (declaring the variable outside the functions.

Comment/Reply (w/o sign-up)

Quatrux
Another solution would be to use a Class for those kind of things, usually if you want other variables work in other function, you create a Class, so the basic example is:

CODE
class MyClass {

    var $var1 = '';

    function myFunction1() { $this->var1 = 'Value'; }
    
    function myFunction2() { return $this->var1; }

}

$MyClass = new MyClass;

echo $MyClass->myFunction2(); // Value


So you can access and manage all the variables using $this->var1; In fact, sometimes using a Class is very comfortable, to play with functions wink.gif

Comment/Reply (w/o sign-up)

demolaynyc
Hey Quatrux, is that Actionscript or PHP? It looks to me like PHP. Well, it can be converted to AS easily anyway. wait, I noticed here:

" $MyClass = new Database "

why would you make a new "Database" rather than "MyClass"? JW


Comment/Reply (w/o sign-up)

lonelym
Use the _root.NAMEOFVARIABLE so that you can grab it later on. Hope it helps.

function myFunction1() { _root.this = 'Value'; }

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Retrieve Variables Function


    Looking for note, retrieve, variables, function

See Also,

*SIMILAR VIDEOS*
Searching Video's for note, retrieve, variables, function
advertisement



Note: How To Retrieve Variables From Function To Another

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com