Android Question Object Names on the Fly

rfhall50

Member
Licensed User
Longtime User
Is it possible to compose an object name on the fly?

Example:

Dim ButtonA as Button
ObjName = "Button" & "A"
ObjName.Text = "This is a test button."

I know you would have to Dimension ObjName, but as what ?

Thank You.
Bob
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
A variable is a reference to an object, not the object itself. You would need to iterate through all the controls in your activity and look for the .Name property you want.

Or, you could create a map and reference the objects by a key you create. For example (not tested, just concept code):
B4X:
Sub Globals
    Dim myButtons As Map   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    myButtons.Initialize
    Dim ThisButton As Button
    ThisButton.Initialize("ThisButton")
    Activity.AddView(ThisButton, 10dip, 10dip, 100dip, 100dip)
    ThisButton.Text = "This Button"
    ' add button to our map
    myButtons.Put("ButtonA", ThisButton)
End Sub

Sub ThisButton_Click
    ' you could (and probably should) use the "Sender" object here
    ' but I am using the map we created to illustrate this concept
    Dim SomeButton As Button = myButtons.Get("ButtonA")
    SomeButton.Text = "That Button"
End Sub
 
Upvote 0

rfhall50

Member
Licensed User
Longtime User
A variable is a reference to an object, not the object itself. You would need to iterate through all the controls in your activity and look for the .Name property you want.

Or, you could create a map and reference the objects by a key you create. For example (not tested, just concept code):
B4X:
Sub Globals
    Dim myButtons As Map  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    myButtons.Initialize
    Dim ThisButton As Button
    ThisButton.Initialize("ThisButton")
    Activity.AddView(ThisButton, 10dip, 10dip, 100dip, 100dip)
    ThisButton.Text = "This Button"
    ' add button to our map
    myButtons.Put("ButtonA", ThisButton)
End Sub

Sub ThisButton_Click
    ' you could (and probably should) use the "Sender" object here
    ' but I am using the map we created to illustrate this concept
    Dim SomeButton As Button = myButtons.Get("ButtonA")
    SomeButton.Text = "That Button"
End Sub
Thank You.
Understand the concept, just need to understand the code. Thanks again.
Bob
 
Upvote 0
Top