Android Question Use string as label name. [Solved]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

I have 16 labels [lblF0 to lblF15] the text of these labels is changed by the user.
I wish to build a list [Item0 to Item15] showing the text of the labels, first thought was a For/Next loop [ i = 0 to 15].

EG
B4X:
private temp as string
For i = 0 to 15
    temp = "lblF"&i
    Item(i) = temp.text
Next

Of course this does not work, temp is a string not the name of the label.
Is there a simple way to use/convert the string as/to the name of the label?

I could go down the path of:
If i = 0 then Item(i) = lblF0.text
If i = 1 then ........

but not elegant.


Regards Roger
 

stevel05

Expert
Licensed User
Longtime User
You can't 'Build' the variable name from text, you can get something similar.

Try This:

B4X:
Dim LblF() as Label = Array as Label(LblF0,LblF1,LblF2,LblF3,LblF4,LblF5.LblF6,LblF7,LblF8,LblF9,LblF10,LblF11,LblF12,LblF13,LblF14,LblF15)

For i = 0 to LblF.Length - 1
    Log(lblF(i).Text)
Next
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
do you mean this?

B4X:
For i = 0 to 15
    Item(i).text = "lblF"&i
    Item(i).tag = "lblF"&i  '<- it's better to use the tag as identifier
Next
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
You can't 'Build' the variable name from text, you can get something similar.

Try This:

B4X:
Dim LblF() as Label = Array as Label(LblF0,LblF1,LblF2,LblF3,LblF4,LblF5.LblF6,LblF7,LblF8,LblF9,LblF10,LblF11,LblF12,LblF13,LblF14,LblF15)

For i = 0 to LblF.Length - 1
    Log(lblF(i).Text)
Next



Error at line 9 ??
I have failed to initialize lblF(). Tried in code, tried in designer.

java.lang.RuntimeException: Object should first be initialized (Label).:
Sub Globals
    ....stuff
    Dim LblF() As Label = Array As Label(lblF0,lblF1,lblF2,lblF3, lblF4,lblF5  ,  lblF6,lblF7,lblF8,lblF9,lblF10,lblF11,lblF12,lblF13,lblF14,lblF15)
   
End Sub

Sub FNumSelect
    For i = 0 To 15    
        Log(LblF(i).Text)
    Next
End Sub
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Private LblF() As Label 'in Globals
Then after you load the layout in Activity_Create: You put the below line:
LblF =Array As Label(lblF0,lblF1,lblF2,lblF3,lblF4,l................
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
You can't 'Build' the variable name from text, you can get something similar.

Try This:

B4X:
Dim LblF() as Label = Array as Label(LblF0,LblF1,LblF2,LblF3,LblF4,LblF5.LblF6,LblF7,LblF8,LblF9,LblF10,LblF11,LblF12,LblF13,LblF14,LblF15)

For i = 0 to LblF.Length - 1
    Log(lblF(i).Text)
Next

stevel05,
Many thanks.

Regards Roger
 
Upvote 0
Top