Page 1 of 1

Global variables inside Procedures

Posted: Fri Aug 23, 2019 9:45 pm
by VB6_to_PBx
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 ?

Re: Global variables inside Procedures

Posted: Fri Aug 23, 2019 10:09 pm
by skywalk
Why not?
I put many def's in Main() or Init().

Re: Global variables inside Procedures

Posted: Fri Aug 23, 2019 10:27 pm
by spikey
You should be aware that there is the potential to create accidentally a scope ambiguity bug:-

Code: Select all

Procedure P1()
  
  V = 1
  
  Debug V
  
EndProcedure

Procedure P2()
  
  Global V.I 
  
  Debug V
  
EndProcedure

P1()
P2()

Debug V
If you run this code you will get the output:-

Code: Select all

1
0
0
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).

Re: Global variables inside Procedures

Posted: Fri Aug 23, 2019 10:54 pm
by Little John

Re: Global variables inside Procedures

Posted: Sat Aug 24, 2019 1:27 am
by VB6_to_PBx
Little John wrote:viewtopic.php?f=13&t=54221
from Fred :
Global is OK in procedures
i'm doing something like this ... i call the 1st Procedure which holds the Global variables
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()