Roger Daley Well-Known Member Licensed User Longtime User Feb 15, 2020 #1 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
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 Feb 15, 2020 #2 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: Feb 15, 2020 Upvote 0
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
S sorex Expert Licensed User Longtime User Feb 15, 2020 #3 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
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
Roger Daley Well-Known Member Licensed User Longtime User Feb 15, 2020 #4 sorex said: 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 Click to expand... Sorry sorex, I was trying to ask. Item(i).text = "lblF" & i.text Bad phrasing on my part. Roger Upvote 0
sorex said: 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 Click to expand... Sorry sorex, I was trying to ask. Item(i).text = "lblF" & i.text Bad phrasing on my part. Roger
Roger Daley Well-Known Member Licensed User Longtime User Feb 15, 2020 #5 stevel05 said: 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 Click to expand... Error at line 9 ?? I have failed to initialize lblF(). Tried in code, tried in designer. [CODE lang="b4x" title="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 [/CODE] Last edited: Feb 15, 2020 Upvote 0
stevel05 said: 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 Click to expand... Error at line 9 ?? I have failed to initialize lblF(). Tried in code, tried in designer. [CODE lang="b4x" title="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 [/CODE]
M Mahares Expert Licensed User Longtime User Feb 15, 2020 #6 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
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................
Roger Daley Well-Known Member Licensed User Longtime User Feb 15, 2020 #7 Mahares said: 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................ Click to expand... Thanks Mahares, It works perfectly. Regards Roger Upvote 0
Mahares said: 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................ Click to expand... Thanks Mahares, It works perfectly. Regards Roger
Roger Daley Well-Known Member Licensed User Longtime User Feb 15, 2020 #8 stevel05 said: 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 Click to expand... stevel05, Many thanks. Regards Roger Upvote 0
stevel05 said: 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 Click to expand... stevel05, Many thanks. Regards Roger