Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Note: How To Retrieve Variables From Function To Another
demolaynyc
post Nov 26 2006, 05:39 AM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 330
Joined: 2-February 06
Member No.: 11,040



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.
Go to the top of the page
 
+Quote Post
seec77
post Nov 26 2006, 11:45 AM
Post #2


Advanced Member
Group Icon

Group: Members
Posts: 157
Joined: 16-May 06
Member No.: 13,476



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.
Go to the top of the page
 
+Quote Post
demolaynyc
post Mar 22 2007, 05:12 PM
Post #3


Premium Member
Group Icon

Group: Members
Posts: 330
Joined: 2-February 06
Member No.: 11,040



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.
Go to the top of the page
 
+Quote Post
Quatrux
post Mar 23 2007, 05:39 AM
Post #4


the Q
Group Icon

Group: [HOSTED]
Posts: 1,022
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



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

This post has been edited by Quatrux: Mar 24 2007, 03:52 AM
Go to the top of the page
 
+Quote Post
demolaynyc
post Mar 24 2007, 02:53 AM
Post #5


Premium Member
Group Icon

Group: Members
Posts: 330
Joined: 2-February 06
Member No.: 11,040



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

Go to the top of the page
 
+Quote Post
lonelym
post Jul 9 2007, 10:40 AM
Post #6


Member - Active Contributor
Group Icon

Group: Members
Posts: 91
Joined: 18-May 07
Member No.: 22,008



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

function myFunction1() { _root.this = 'Value'; }
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Flash, Mysql And Php(4)
  2. Sending Mail Using PHP's Mail() Function(2)
  3. Sending Authorised Mail Using Imap_mail Function(2)
  4. How To #3: Custom BBCdes For This Board (Note)(15)
  5. Php : Variables Included Dont Work In Functions(4)
  6. Function Programming - About Function Languages(1)
  7. A Note To All Illegal Windows Xp Owners(47)
  8. Question About Asp Now() Function(6)
  9. Another Bad Note For Bush(10)
  10. Looking For Function: Imagecreatetruecolor (php-gd(7)
  11. Upgraded The Note Bbcode(4)
  12. The Edit Function That Is Now Available!(4)
  13. Calling Of Functions Between Mulitple External Javascript Files(2)
  14. Using The Php Mail() Function For Images Or Attachments(3)
  15. Photoshop Tutorial: Cutting Out A Psd(3)
  1. User Authentication Session Handling Problems(14)
  2. Dell Laptop Batteries - Explode Into Flames(14)
  3. Quickly Create Form Variables(5)
  4. Pascal For Beginners - Part One(7)
  5. C# Tutorial : Lesson 2 - Variables & Operators(0)
  6. Help Spliting A String Into 3 Variables In C++(2)
  7. $_get Issue (urgent!)(11)
  8. Php Long Variables(5)
  9. Include Function For Javascript(7)
  10. Copy To Clipboard Function(5)
  11. Range Function(9)
  12. Preventing Spam When Using Php's Mail Function(5)
  13. Calendar And The Date () Function(0)


 



- Lo-Fi Version Time is now: 6th September 2008 - 01:29 AM