Page 1 of 1

Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 2:37 pm
by jacdelad

Code: Select all

Global MyVar=1

Procedure Test()
  Protected MyVar=2
  Debug MyVar
EndProcedure

Test()
Is it possible to get access on the global variable from within the procedure while the local one is defined?

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 2:51 pm
by Little John
 

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 3:34 pm
by #NULL
You could create an alias via a pointer to access it with a different name:

Code: Select all

Global MyVar=1
Global *MyVar_alias.Integer = @MyVar

Procedure Test()
  Protected MyVar=2
  Debug MyVar           ; 2
  *MyVar_alias\i + 100
  Debug *MyVar_alias\i  ; 101
EndProcedure

Test()
Debug MyVar             ; 101

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 3:56 pm
by skywalk
I am not a fan of naked global variables.
Why not use a globally defined structure and access its members anywhere?

Code: Select all

Structure MyGlobalVars
  myvar.i
  myvar2.i
  ;..
EndStructure
Global mgv.MyGlobalVars

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 4:16 pm
by Demivec
The solutions given so far would work. The real issue is choosing better variable names. If a procedure needs to access a global variable it should not name any of its local variables with the same name.

Some ways to avoid conflicts include adding a prefix such as 'g_' to the names of global variables and another is to group all goal variables under a single structured variable.

Code: Select all

Global g_myVar, g_myPoint.point, g_myString.s

Structure global_vars
  myVar.i
  myPoint.point
  myString.s
EndStructure

Global global_vars.global_vars

@skywalk: you beat my post while mine was in progress. :)

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 6:56 pm
by Little John
Demivec wrote: Wed Apr 30, 2025 4:16 pm The real issue is choosing better variable names. If a procedure needs to access a global variable it should not name any of its local variables with the same name.
Yep, exactly!
Demivec wrote: Wed Apr 30, 2025 4:16 pm Some ways to avoid conflicts include adding a prefix such as 'g_' to the names of global variables
That's what I'm doing. And I'm adding the prefix 's_' to the names of shared variables.

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 9:30 pm
by jacdelad
Thanks for the answers. So, it's not possible without some tricks. No problem.

Just a sidenote: I just wanted to kno whether I oversaw something or not (like a reverse "Shared"). It's interesting how you all instantly give advise how to avoid it. :twisted:

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 9:48 pm
by Quin
jacdelad wrote: Wed Apr 30, 2025 9:30 pm Just a sidenote: I just wanted to kno whether I oversaw something or not (like a reverse "Shared"). It's interesting how you all instantly give advise how to avoid it. :twisted:
I think that's just because it's very bad practice and something that's quite rare. I've never had this come up across multiple large PB codebases. I don't even use any special naming convention for my globals, my 10K+ LOC application has aaround 5-7 globals, a few of them being structures. Quite clean and readable IMO :)

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 10:09 pm
by jacdelad
My code is unreadable and chaotic. Without having the need for I just stumbled across this question. :mrgreen:
Same with: Why is CountCPUs a procedure and not two constants? Can they change while a program is running?

Re: Access global variable while local one with the same name is declared

Posted: Wed Apr 30, 2025 10:20 pm
by Demivec
jacdelad wrote: Wed Apr 30, 2025 9:30 pm It's interesting how you all instantly give advise how to avoid it. :twisted:
The other solutions were given already so I touched on the one that hasn't been mentioned yet when I posted.

Re: Access global variable while local one with the same name is declared

Posted: Thu May 01, 2025 7:10 am
by #NULL
jacdelad wrote: Wed Apr 30, 2025 10:09 pmWhy is CountCPUs a procedure and not two constants? Can they change while a program is running?
CPUs/Cores can be switch on/off, maybe even during runtime, at least in VMs probably. But the main reason is that constants resolve at compiletime, so they would only reflect the state of your development machine. If your compiled program runs on different computers, it needs a function to get this information at runtime.

Re: Access global variable while local one with the same name is declared

Posted: Thu May 01, 2025 7:32 am
by #NULL
For fun: you can also use the runtime library to access the global as a string name:

Code: Select all

Global MyVar=1
Runtime MyVar
Debug GetRuntimeInteger("MyVar")

Procedure Test()
  Protected MyVar=2
  Debug MyVar
  Debug GetRuntimeInteger("MyVar")
EndProcedure

Test()
With macros you can also use the original identifier and convert it to a string automatically:

Code: Select all

Global MyVar=1
Runtime MyVar

Macro dq
  "
EndMacro

Macro Global(var)
  GetRuntimeInteger(dq#var#dq)
EndMacro

Procedure Test()
  Protected MyVar=2
  Debug MyVar            ; 2
  Debug Global(MyVar)    ; 1
EndProcedure

Test()
- the macro Global() doesn't seem to interfere with the regular Global keyword, but i would not advise this.
- to change the value you would need a second macro using SetRuntimeInteger()
- readability does *not* increase, to say the least :) but it's always interesting to see what's possible.

Re: Access global variable while local one with the same name is declared

Posted: Thu May 01, 2025 8:05 am
by jacdelad
#NULL wrote: Thu May 01, 2025 7:10 am
jacdelad wrote: Wed Apr 30, 2025 10:09 pmWhy is CountCPUs a procedure and not two constants? Can they change while a program is running?
CPUs/Cores can be switch on/off, maybe even during runtime, at least in VMs probably. But the main reason is that constants resolve at compiletime, so they would only reflect the state of your development machine. If your compiled program runs on different computers, it needs a function to get this information at runtime.
Yeah, I suspected that. Some other languages have "dynamic" constants which are replaced on run. No problem here.

Thanks #NULL and I really appreciate all your creativity. I was just curious whether there's a predefined way to do this. Never wanted to use this.