Variable by name question

Just starting out? Need help? Post your questions and find answers here.
droadje
User
User
Posts: 12
Joined: Thu Mar 28, 2019 6:02 pm
Location: Netherlands, Ter Apel
Contact:

Variable by name question

Post by droadje »

Been looking for hours, but cannot find it:

The button is defined with #PB_Any and referenced by variable Button1_0
As i have many buttons, i would like to be able to play around with the names chosen.

Difficult to explain, but how can i use the integer variable name (Button1_0) from a string.
So for example i have a variable usevar.s which has "Button1_0" in it.
Howto convert my string variable to show the content of the containing int variable.

Perhaps with a example to clear it up:

Code: Select all

; I have this button defined:
Button1_0 = ButtonGadget(#PB_Any,130, 50, 30, 25, "", #PB_Button_Left)

; This is working, of course:
SetGadgetText(Button1_0, "E")

; But how do i get this to work?
usevar.s = "Button1_0"  ; a string
SetGadgetText(usevar, "E")  ; not working of course, because it is expecting that uservar is not a string but integer, howto convert?


infratec
Always Here
Always Here
Posts: 7676
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Variable by name question

Post by infratec »

There is no native Eval() in PB
And since it is a compiler and not an interpreter it is a bit difficult.

Here is a little trick:

Code: Select all

NewMap GadgetMap.i()

; I have this button defined:
GadgetMap("Button1_0") = ButtonGadget(#PB_Any,130, 50, 30, 25, "", #PB_Button_Left)

; This is working, of course:
SetGadgetText(GadgetMap("Button1_0"), "E")

; But how do i get this to work?
usevar.s = "Button1_0"  ; a string
SetGadgetText(GadgetMap(usevar), "E")
I hope it works.
Since you did not provide a complete example, I didn't test it.
wombats
Enthusiast
Enthusiast
Posts: 722
Joined: Thu Dec 29, 2011 5:03 pm

Re: Variable by name question

Post by wombats »

You can use the Runtime library:

Code: Select all

OpenWindow(0, 0, 0, 320, 240, "", #PB_Window_SystemMenu)

Define Button1_0.i
Runtime Button1_0
Button1_0 = ButtonGadget(#PB_Any, 10, 10, 50, 25, "", #PB_Button_Left)

usevar.s = "Button1_0"  ; a string
SetGadgetText(GetRuntimeInteger(usevar), "E")

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
droadje
User
User
Posts: 12
Joined: Thu Mar 28, 2019 6:02 pm
Location: Netherlands, Ter Apel
Contact:

Re: Variable by name question

Post by droadje »

Thanks guys,

Your examples are working, now i understand that i have to go a different path....
Post Reply