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

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Little John
Addict
Addict
Posts: 4802
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post by Little John »

 
Last edited by Little John on Thu May 01, 2025 9:54 am, edited 1 time in total.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

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

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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. :)
Little John
Addict
Addict
Posts: 4802
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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:
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post 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 :)
User avatar
jacdelad
Addict
Addict
Posts: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

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

Post 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.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

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

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply