Page 1 of 1

Variable by name question

Posted: Fri Apr 05, 2019 6:23 pm
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?



Re: Variable by name question

Posted: Fri Apr 05, 2019 6:37 pm
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.

Re: Variable by name question

Posted: Fri Apr 05, 2019 6:39 pm
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

Re: Variable by name question

Posted: Fri Apr 05, 2019 7:08 pm
by droadje
Thanks guys,

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