Android Question Adding a variables value to an objects name to access it

ggpanta

Member
Licensed User
Longtime User
Out of curiosity, while helping a guy in the chat, in his code he had something like this:

Dim i as Int
label(i).Text = "foo"

Is there any way in B4A to actually do that?

Instead I proposed he just parses all panels, check for labels in the list and update them like that (which should be the optimal way). But wanted to know if B4A has a function that would allow something similar to that.
 

DonManfred

Expert
Licensed User
Longtime User
It is not possible the way you want.

Create a array of labels and put the labels in the array.
You can use this array then to access the labels.

Search the forum for additional answers
 
Upvote 0

ggpanta

Member
Licensed User
Longtime User
It is not possible the way you want.

Create a array of labels and put the labels in the array.
You can use this array then to access the labels.

Search the forum for additional answers
Yep that I get how it would work, I was just asking if there was a special case were what he was trying would work.
 
Upvote 0

ggpanta

Member
Licensed User
Longtime User
Manfred, just read my OP :)

If it helps you with the original question this is the label he was trying to access Dim Label1 As Label .
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim top As Int   
    Dim labels(5) As Label
    For i = 0 To 4
        labels(i).Initialize("Label")
        If i = 0 Then top = 0
        If i = 1 Then top = 20%y
        If i = 2 Then top = 40%y
        If i = 3 Then top = 60%y
        If i = 4 Then top = 80%y
        Activity.AddView(labels(i),0,top,100%x,20dip)
       
    Next
   
    Dim r As Int = Rnd(0,5)
    labels(r).Text = "Hallo"
 
Upvote 0

ggpanta

Member
Licensed User
Longtime User
Manfred I dont want that solution mate, I already had the solution I proposed to that guy in my OP just for this case where people would propose alternatives.

He is using the designer, has the labels in a panel, your solution need a lot more maintenance and it even was suggested to him by others.

In ANY case I even said thank you for your solution when you mentioned it. My OP was prety clear, from the title to the text. You answered already in the first line of your first reply that it cant be done and thats the only think that was asked.

And again thanks for your numerous replies.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use a Map.
B4X:
'build the map
Activity.LoadLayout(...)
For Each v As View In Activity.GetAllViewsRecursive
MapOfViews.Put(v.Tag, v) 'add the names to the Tag property of the views you want to access
Next

Dim label5 As Label = MapOfViews.Get("label5")
'Or
GetView("label5").Color = Colors.Red

Sub GetView(Name As String) As View
Return MapOfViews.Get(Name)
End Sub
 
Upvote 0

ggpanta

Member
Licensed User
Longtime User
Something very similar was suggested Erel, the only difference was the use of a tag per label since the database fields he was querying where of various types and it was more maintainable that way.
 
Upvote 0
Top