Android Question declare some similar variables

hi everyone.
i want to write some codes to declare similar variables. let me say more intelligile. i want to declare btn1 , btn2 , btn3 , .... and btn100 as button.
i enterd this code in IDE but it didnt admit:
codes:
For i=0 to 99
    dim btn&i as button
Next
do you have any suggestions?
 

LucaMs

Expert
Licensed User
Longtime User
It is better to create Views (also Buttons, therefore) using the Designer.

If you really want to create Buttons by code, it is not necessary that they have different names; their management takes place in the Click event, being able to distinguish which of them has been pressed using the Tag property (set in the example below) or the text.

B4X:
For i = 0 to 99
    Dim btn As Button
    btn.Initialize("btn")
    'Here you have to add btn to Activity, or Root, or a Panel
    'Root.AddView(btn, ....) <--- Root in B4XPages projects.
    btn.Tag = i
    btn.Text = "b " & i
Next i

Private btn_Click
    Dim b As Button = Sender
    Log("Button Tag = " & b.Tag)
    Log("Button Text = " & b.Text)
End Sub
 
Upvote 2
thanks so much. i saw this method(distinguish them with tag property) in others questions ,but i want to know really isnt exist any method to declaring some variable for several times?
thnaks for your replys.
 
Upvote 0
Top