is it OK to place Global variables inside Procedures ?
some of these same Global variables would then be accessed in other Procedures in my Program .
anyone see potential problems with doing this ?
Global variables inside Procedures
- VB6_to_PBx
- Enthusiast
- Posts: 617
- Joined: Mon May 09, 2011 9:36 am
Global variables inside Procedures
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Global variables inside Procedures
Why not?
I put many def's in Main() or Init().
I put many def's in Main() or Init().
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Global variables inside Procedures
You should be aware that there is the potential to create accidentally a scope ambiguity bug:-
If you run this code you will get the output:-
The variable V isn't truly global despite appearing to be. P1 will implicitly create a local variable, which will be inaccessible to other procedures.
(Putting EnableExplict at the top of your code will detect this error, but if your code isn't currently written to allow that you'd need a refactor).
Code: Select all
Procedure P1()
V = 1
Debug V
EndProcedure
Procedure P2()
Global V.I
Debug V
EndProcedure
P1()
P2()
Debug V
Code: Select all
1
0
0
(Putting EnableExplict at the top of your code will detect this error, but if your code isn't currently written to allow that you'd need a refactor).
-
- Addict
- Posts: 4519
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
- VB6_to_PBx
- Enthusiast
- Posts: 617
- Joined: Mon May 09, 2011 9:36 am
Re: Global variables inside Procedures
from Fred :Little John wrote:viewtopic.php?f=13&t=54221
i'm doing something like this ... i call the 1st Procedure which holds the Global variablesGlobal is OK in procedures
then call the other Procedures that calculate data
Code: Select all
EnableExplicit
Procedure SetGlobalVar()
Global var=1
EndProcedure
Procedure ShowGlobalVar()
Debug var
EndProcedure
SetGlobalVar()
ShowGlobalVar()
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison