Page 1 of 1

Variable handling?

Posted: Wed Oct 26, 2005 4:53 pm
by Rocket Boy
I need to know how I can not lose the value of a variable when leaving a sub, so that I can go back to it and access that value again.

Posted: Wed Oct 26, 2005 4:54 pm
by Z!re
look up the STATIC keyword.. or the SHARED keyword..

Posted: Wed Oct 26, 2005 5:43 pm
by Patz QuickBASIC Creations

Code: Select all

DECLARE SUB yoursub ()
DIM SHARED yourvariable
CALL yoursub
PRINT yourvariable

SUB yoursub()
LET yourvariable = 4
END SUB

Posted: Wed Oct 26, 2005 6:48 pm
by moneo
A more common method is to use a parameter list with the sub, like so:

Code: Select all

DECLARE SUB yoursub (yourvariable)
CALL yoursub (subvariable)
PRINT subvariable

END

SUB yoursub (yourvariable)
LET yourvariable = 4
END SUB
The SUB doesn't use the value of "subvariable" from the CALL, which the SUB refers to as "yourvariable".
However, it does modify "yourvariable" to 4 which will be returned to the CALL with "subvariable" equal to 4.
*****

Posted: Thu Oct 27, 2005 2:29 pm
by {Nathan}
You can also use the common statement, but thats really messy and un-good... use it like this... i ferget how to use it though, so check qbs help.[/code]

Posted: Thu Oct 27, 2005 6:25 pm
by moneo
Nathan1993 wrote:You can also use the common statement, but thats really messy and un-good... use it like this... i ferget how to use it though, so check qbs help.[/code]
Here's the definition of the COMMON statement:

Code: Select all

COMMON - a non-executable statement that declares global variables for 
         sharing between modules, or for chaining to another program
It does not apply to the issue of this thread.
*****

Posted: Thu Oct 27, 2005 6:53 pm
by {Nathan}
yes it does (kinda) because it makes a global varible and a global varible doesn't loose its value when you exit a sub that it is used in...

:roll: :roll: :roll:

i think, anyway...

:shock: :shock: :? now im just confusing myself...