Page 1 of 1

Need help with subs!!!

Posted: Sun May 07, 2006 2:51 pm
by crazydrivr
I am making an RPG game that uses money, and I put the shops in a sub. I need a way to total the money i picked up in one sub or the main module and transfer it to the shop subs. So basically a way to transfer a variable from one sub to another.

Posted: Sun May 07, 2006 3:51 pm
by {Nathan}
You could:

-Use COMMON
-Use DIM SHARED
-Use STATIC varibles
-Convert your subs to a function (I recommend this: easiest to work with)

If you don't know any of those statements, look on QB's online help or search google.

Posted: Sun May 07, 2006 7:44 pm
by Theophage
What you've got to understand is the difference between a local variable and a global variable. When your program is all in one module, both types act the same, but when your program is broken up into a main module and subs, the difference becomes apparent.

Any variable used in either the main module or any of the subs is automatically a local variable. This means that only this section of the program can use the value of this variable. If you've got a variable like Gold=200 in your main program, and you try to say print Gold in one of your subs, the sub would print zero.

A global variable is one that all parts of the program recognize. You define these in your main module like this:

DIM SHARED Gold AS INTEGER

Now that the variable "Gold" is shared, you can assigning it a value in any part of your program, and any other part of your program can use that value.