Android Question For Each with Arrays

Midimaster

Active Member
Licensed User
I defined an array of user type with 99 elements
B4X:
    Type MusicEventTyp(SoundNr As Int, Volume As Float)
    Private Music(99) As MusicEventTyp
and i filled 10 of them, but not all:

B4X:
...
    Music(9)= Init(6, 0.9)
...
Sub Init(Nr As Int, Volume As Float) As MusicEventTyp
    Dim loc As MusicEventTyp
    loc.Initialize
    loc.SoundNr=Nr
    loc.volume=Volume
    Return loc
End Sub

now i want to iterate through the initalized ones, but it looks like the loop iterates through all 99:
B4X:
    For Each loc As MusicEventTyp In Music
        Log("SoundNr" & loc.SoundNr)
    Next

does this mean, that all 99 elements are initialized from the first moment?

As a list is also not possible with user defined types... is there are chance to only iterate through the 10 defined?
 
Last edited:

Midimaster

Active Member
Licensed User
the mistakes came only from copying the longer code to a essential code for the forum... I corrected it.

I read anywhere in the forum that lists of user defined types are not possible in B4A. I of course would prefer lists!!! So I will try it now and report....
 
Upvote 0

Midimaster

Active Member
Licensed User
thank you for your quick answer... it works and it looks much better than the "workaround" solution with array:
B4X:
    Type MusicEventTyp(SoundNr as Int, Volume As Float)
    Private Music As List
...
    Init(6,0.9)
....

Sub Init(Nr As Int, Volume As Float) As MusicEventTyp
    Dim loc As MusicEventTyp
    loc.Initialize
    loc.SoundNr=Nr
    loc.Volume=Volume
    Music.Add(loc)
End Sub
....

    For Each loc As MusicEventTyp In Music
            Log("SoundNr" & loc.SoundNr)
    Next
 
Upvote 0
Top