Android Question [Solved] for each with a map: error

Pflichtfeld

Active Member
Licensed User
I try to create a map with a userdefined type and log the items with for each:
B4X:
Dim gr(2) As Grinder_Values   'user defined type
gr(0).Initialize()
gr(0)=CreateGrinder_Values(17.5, 71, 200,12,12,"",0,False)
gr(1).Initialize
gr(1)=CreateGrinder_Values(66,30,200,12,12,"",0,False)
grinders=CreateMap("Name1":gr(0),"Name2":gr(1))
For Each gg In grinders
        Log (gg)
Next

in the for each line I get the errormessage from compiler:
Kompiliere generierten Java Code. Error
B4A line: 41
For Each gg In grinders
src\Test\TH\starter.java:311: error: incompatible types: Map cannot be converted to IterableList
final anywheresoftware.b4a.BA.IterableList group18 = _grinders;


1. In general: Is it possible to use userdefined types as value in maps?
2. Is it not possible, to iterate through a map?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Of course.
2. It is possible but you should do it correctly.
3. You don't need to call Initialize here. It will be called inside CreateGrinder_Values.
B4X:
For Each gg As Grinder_Values In grinders.Values

Next
4. Better to use List instead of an array.
5. Watch the collections video tutorial: https://www.b4x.com/etp.html
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thank you Erel!
4. Better to use List instead of an array.
Did you mean it this way:
B4X:
Dim gl As List
        gl.Initialize
        gl.Add(CreateGrinder_Values(17.5, 71, 200,12,12,"",0,False))
        gl.Add(CreateGrinder_Values(66,30,200,12,12,"",0,False))
        grinders=CreateMap("Tormek T4 - support horizontal":gl.Get(0),"Tormek T4 - support vertical":gl.Get(1))
what would be the benefit of this version compared to the array-version?

5. Watch the collections video tutorial: https://www.b4x.com/etp.html
watched it once, now second time. You describe the map.keys and map.values-iteration. But that it is impossible to iterate just over map is something I learned through this post.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But that it is impossible to iterate just over map is something I learned through this post.
There is no such thing. You can either iterate over the keys or over the values. You need to choose.

what would be the benefit of this version compared to the array-version?
Lists are more powerful and in most cases should be the default choice.
 
Upvote 0
Top