Getting name of a variable as a string$ during runtime?

Just starting out? Need help? Post your questions and find answers here.
User avatar
diceman
User
User
Posts: 74
Joined: Tue Apr 10, 2018 9:42 pm

Getting name of a variable as a string$ during runtime?

Post by diceman »

I wonder if this is possible ... :)

I declared a variable:

Code: Select all

Global foo
Now I want to output the name of the variable as a string$, so when running the program, in the Debugger window it should print:

Code: Select all

foo
Thanks!
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
AZJIO
Addict
Addict
Posts: 2235
Joined: Sun May 14, 2017 1:48 am

Re: Getting name of a variable as a string$ during runtime?

Post by AZJIO »

Map?
In compiled languages, the name is not stored. But you can make a connection through "Map".
Last edited by AZJIO on Mon Nov 03, 2025 11:49 am, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18372
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Getting name of a variable as a string$ during runtime?

Post by Fred »

I don't think it's possible right now, what you want it something like nameof() in C#
User avatar
diceman
User
User
Posts: 74
Joined: Tue Apr 10, 2018 9:42 pm

Re: Getting name of a variable as a string$ during runtime?

Post by diceman »

OK, thanks, thought so. :)
But interesting nonetheless, that C# can do that.
No biggie. Was just asking, because I'm currently coding the custom keyboardMapping routine in my game.
Gotta do this manually then with a "buttonID translation routine". :D
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
User avatar
jacdelad
Addict
Addict
Posts: 2040
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Getting name of a variable as a string$ during runtime?

Post by jacdelad »

AZJIO wrote: Mon Nov 03, 2025 11:46 am Map?
In compiled languages, the name is not stored. But you can make a connection through "Map".
How does the dialog library connect the variable name in a string to the actual variable?
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
BarryG
Addict
Addict
Posts: 4239
Joined: Thu Apr 18, 2019 8:17 am

Re: Getting name of a variable as a string$ during runtime?

Post by BarryG »

User avatar
TI-994A
Addict
Addict
Posts: 2756
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Getting name of a variable as a string$ during runtime?

Post by TI-994A »

diceman wrote: Mon Nov 03, 2025 12:11 pm...Gotta do this manually then with a "buttonID translation routine". :D
Just curious: what would the "button ID translation routine" do, exactly?
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
diceman
User
User
Posts: 74
Joined: Tue Apr 10, 2018 9:42 pm

Re: Getting name of a variable as a string$ during runtime?

Post by diceman »

TI-994A wrote: Mon Nov 03, 2025 12:29 pmJust curious: what would the "button ID translation routine" do, exactly?
I can call the procedure with a String (e.g. "#PB_Keyboard_Return") and then get in return the respective buttonID (#PB_Keyboard_Return), and another procedure with a vice versa functionality (pass a variable, and get the corresponding String for display).

@Barry
The GetRuntimeInteger()-thing certainly looks interesting, thanks!
Maybe when I get to do this again sometime, for now I'm satisfied with my "dirty" solution. Wasn't really a problem to solve, just boring copy/paste and more "paperwork" stuff. Not very elegant, but gets the job done. 8)
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
User avatar
mk-soft
Always Here
Always Here
Posts: 6363
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Getting name of a variable as a string$ during runtime?

Post by mk-soft »

Update ;)

Code: Select all


Macro _dq_
  "
EndMacro

Macro MyDebug(_value_, _proc_=#PB_Compiler_Procedure, _line_=#PB_Compiler_Line)
  Debug "Proc "+_proc_+" / Line "+_line_+": Variable "+_dq_#_value_ = _dq_ + _value_
EndMacro

Global foo = 100

MyDebug(foo)

Procedure MyFunction()
  Protected iValue = 1000
  MyDebug(iValue)
EndProcedure

MyFunction()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 2235
Joined: Sun May 14, 2017 1:48 am

Re: Getting name of a variable as a string$ during runtime?

Post by AZJIO »

jacdelad wrote: Mon Nov 03, 2025 12:23 pm How does the dialog library connect the variable name in a string to the actual variable?
AutoIt3 has the Assing() and Eval() functions, one creates a variable from a string, the other reads a variable from a string. I once needed access to a list of variables, and I created it using these functions before I knew what an array was. You can store a name in a map and access its value. Just create an example, even if it doesn't work, and I'll convert it into an example using a map.

Code: Select all

name$ = "5"
debug nameof(name$)

AddMapElement(Map(), "name$")
Map() = "5"
debug Map()
User avatar
diceman
User
User
Posts: 74
Joined: Tue Apr 10, 2018 9:42 pm

Re: Getting name of a variable as a string$ during runtime?

Post by diceman »

Interesting, thank you! :shock:
Now these points of data make a beautiful line,
And we're out of Beta, we're releasing on time.
User avatar
Piero
Addict
Addict
Posts: 1077
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Getting name of a variable as a string$ during runtime?

Post by Piero »

mk-soft wrote: Mon Nov 03, 2025 12:56 pm Update ;)

Code: Select all

CompilerIf #PB_Compiler_Debugger
   Macro _dq_ : "
   EndMacro
   Macro MyDebug(_value_, assert=1, _proc_="PROC "+#PB_Compiler_Procedure+" / ", _line_="LINE "+#PB_Compiler_Line+" /")
      If assert
         Debug _proc_+_line_+" VAR "+_dq_#_value_ = _dq_ + _value_
      Else
         Debug "LINE "+#PB_Compiler_Line+": "+_dq_#assert#_dq_+" is not true!"
         CallDebugger ; "Breakpoint"
      EndIf
   EndMacro
CompilerElse
   Macro MyDebug(a,b=,c=,d=) : EndMacro
CompilerEndIf

Global foo = 101

MyDebug(foo,1,"")
MyDebug(foo, foo>100, "All OK; foo is greater than 100:","")

Procedure MyFunction()
   Protected iValue = 1000
   MyDebug(iValue)
EndProcedure

MyFunction()
;)
Post Reply