Build variable from string/int

SpaceCow

Member
Licensed User
Longtime User
Hi,

Is it possible to call a object from a string? For example i have "button1" in a string, how do i transform this to call the object?

Thanks!
 

SpaceCow

Member
Licensed User
Longtime User
I have the buttons: button1, button2, button3

Now i want to call the buttons by the number so i have to merge the number with the text "button".

Something like this:

Sub Call_Button (BtnNumber As Int)
Button+BtnNumber.Visible = true
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you defined the buttons in the Designer for each button enter 'Buttons' in the EventName field and enter the number of the button in the Tag field.
If you defined the buttons in the code you must initialize them with
B4X:
Button1.Initialize("Buttons")
Button1.Tag = 1
Button2.Initialize("Buttons")
Button2.Tag = 2
' etc.
That means all buttons point to the same event routines.
Then you have to use the code below in the event routine:
B4X:
Sub Buttons_Click
    Dim btn As Button     ' defines a Button
    
    btn = Sender          'sets btn to the Button that raised the event
    Select btn.Tag        ' btn.Tag is the Button number
    Case 1
        .                 ' code for Button1
    Case 2
        .                 ' code for Button2
    Case n
        .                    
    End Select
End Sub
You could have a look at page 39 in the Beginner's Guide.

Best regards.
 
Upvote 0

SpaceCow

Member
Licensed User
Longtime User
That is not what i want.

I just want to call an object (buttons, images, panels) from a string. In PHP i use this:

$variable = "test";
echo ${"variable"};

Above outputs the string "test".
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
I suppose that if you want to simulate that behaviour then you also could create a Map, with the names as keys, and the objects as values.
 
Upvote 0
Top