|
|
|
| Web Hosting Guide |
![]() ![]() |
Note: How To Retrieve Variables From Function To Another |
Nov 26 2006, 05:39 AM
Post
#1
|
|
|
Premium Member 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. |
|
|
|
Nov 26 2006, 11:45 AM
Post
#2
|
|
|
Advanced Member 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. |
|
|
|
Mar 22 2007, 05:12 PM
Post
#3
|
|
|
Premium Member 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.
|
|
|
|
Mar 23 2007, 05:39 AM
Post
#4
|
|
|
the Q Group: [HOSTED] Posts: 1,314 Joined: 13-July 05 From: Lithuania, Vilnius Member No.: 7,059 myCENTs:14.53 |
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 This post has been edited by Quatrux: Mar 24 2007, 03:52 AM |
|
|
|
Mar 24 2007, 02:53 AM
Post
#5
|
|
|
Premium Member 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 |
|
|
|
Jul 9 2007, 10:40 AM
Post
#6
|
|
|
Member - Active Contributor 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'; } |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
55 | prosorcerer | 11,838 | 10th November 2009 - 10:29 PM Last post by: iG-bert |
|||
![]() |
1 | xboxrulz | 331 | 7th February 2009 - 12:38 PM Last post by: rnd-am |
|||
![]() |
11 | TavoxPeru | 2,200 | 29th January 2009 - 07:39 AM Last post by: TavoxPeru |
|||
![]() |
0 | xboxrulz | 300 | 29th November 2008 - 08:37 PM Last post by: xboxrulz |
|||
![]() |
6 | Houdini | 5,765 | 21st November 2008 - 10:37 AM Last post by: magiccode9 |
|||
![]() |
4 | khalilov | 1,583 | 10th November 2008 - 05:19 PM Last post by: khalilov |
|||
![]() |
0 | khalilov | 520 | 9th November 2008 - 11:10 AM Last post by: khalilov |
|||
![]() |
5 | yordan | 4,024 | 6th November 2008 - 09:52 AM Last post by: yordan |
|||
![]() |
0 | CheckProgs | 352 | 12th October 2008 - 02:07 AM Last post by: CheckProgs |
|||
![]() |
7 | vizskywalker | 4,041 | 7th July 2008 - 11:03 AM Last post by: TavoxPeru |
|||
![]() |
6 | jedipi | 3,599 | 14th June 2008 - 08:23 AM Last post by: iGuest |
|||
![]() |
0 | Jared | 351 | 21st April 2008 - 04:40 AM Last post by: Jared |
|||
![]() |
4 | OpaQue | 2,411 | 25th March 2008 - 10:08 PM Last post by: Umar Shah |
|||
![]() |
9 | PerHapsYouSeen | 873 | 29th December 2007 - 02:29 AM Last post by: TavoxPeru |
|||
![]() |
5 | pbolduc | 3,386 | 20th December 2007 - 12:02 PM Last post by: TavoxPeru |
|||
|
Lo-Fi Version | Time is now: 22nd November 2009 - 03:15 PM |
© 2009 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Nov 26 2006, 05:39 AM





