B4J Question Add all items in a type to a list?

LWGShane

Well-Known Member
Licensed User
Longtime User
I have a type which contains level names, scores, etc and would like to add the names to a list. The list will then be loaded into a ComboBox.

Example Type Code:
B4X:
   Public TreasureLevels(16) As CollectTreasure 'Work In Progress
    TreasureLevels(0).Name = "Tropical Forest"
    TreasureLevels(0).Score = 2110
    TreasureLevels(0).BaseTime = 420 '7 Minutes
    TreasureLevels(1).Name = "Silver Lake"
    TreasureLevels(1).Score = 2920
    TreasureLevels(1).BaseTime = 480 '8 Minutes
    TreasureLevels(2).Name = "Thirsty Desert"
    TreasureLevels(2).Score = 3340
    TreasureLevels(2).BaseTime = 600 '10 Minutes
    TreasureLevels(3).Name = "Twilight Hollow"
    TreasureLevels(3).Score = 3240
    TreasureLevels(3).BaseTime = 600 '10 Minutes
    TreasureLevels(4).Name = "Shaded Garden"
    TreasureLevels(4).Score = 3180
    TreasureLevels(4).BaseTime = 480

I would like to all of the names into a List.
 

stevel05

Expert
Licensed User
Longtime User
Try this:
B4X:
Dim ScoreNames As List
ScoreNames.Initialize
For Each TL As CollectTreasure In TreasureLevels
    ScoreNames.Add(TL.Name)
Next

Or

B4X:
Dim ScoreNames As List
ScoreNames.Initialize
For i = 0 to TreasureLevels.Length - 1
    ScoreNames.Add(TreasureLevels(i).Name)
Next
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
Dim ScoreNames As List
ScoreNames.Initialize
For Each TL As CollectTreasure In TreasureLevels
    ScoreNames.Add(TL.Name)
Next

Or

B4X:
Dim ScoreNames As List
ScoreNames.Initialize
For i = 0 to TreasureLevels.Length - 1
    ScoreNames.Add(TreasureLevels(i).Name)
Next

Thanks! I'm trying to make my code as portable as possible so all I have to re-code is the GUI code.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim data() As Object = Array("Tropical Forest", 2110, 420, _
"Silver Lake", 2920, 480, _
"Thirsy Desert", 3340, 600, _
"Twilight Hollow", 3240, 600, _
"Shaded Garden", 3180, 480)
Dim Scores As Map
Scores.Initialize
For i = 0 To data.Length-1 Step 3
   Dim tl As CollectTreasure
   tl.Initialize
   tl.Name = data(i)
   tl.Score = data(i + 1)
   tl.BaseTime = data(i + 2)
   Scores.Put(tl.Name, tl)
Next
 
Upvote 0
Top