Android Question Problem with Map.put

wes58

Active Member
Licensed User
Longtime User
I have a problem with Map

The following is part of the code:
B4X:
   Log("List1 " & list1)
            Log("List2 " & list2)
            instMap.Initialize
            For i = 0 To list1.Size-1
                insteonDev.Initialize
                insteonDev.Name = list2.Get(i)
                insteonDev.Address = list1.Get(i)
                instMap.Put(insteonDev.Name, insteonDev)
                Log(i & insteonDev )
            Next
            Log("instMap " & instMap)

where instMap and insteonDev are declared as follows:
B4X:
    Type InsteonDevList ( _
        Name As String, _
        Address As String)
    Dim insteonDev As InsteonDevList
    Dim instMap As Map

The list items are not added to the map as the log show:
B4X:
List1 (ArrayList) [1FEFCD, 1FEFAF, 1FEF4B, 1FEF65, 1FEF66, 1FEF57, 1FF40C, 1C4B5A, 1C4AB5, 1C4B53, 14B35A]
List2 (ArrayList) [SW1, SW2, SW3, SW4, SW5, SW6, MOT1, REM1, REM2, REM3, SEN1]
0[Address=1FEFCD, IsInitialized=true, Name=SW1]
1[Address=1FEFAF, IsInitialized=true, Name=SW2]
2[Address=1FEF4B, IsInitialized=true, Name=SW3]
3[Address=1FEF65, IsInitialized=true, Name=SW4]
4[Address=1FEF66, IsInitialized=true, Name=SW5]
5[Address=1FEF57, IsInitialized=true, Name=SW6]
6[Address=1FF40C, IsInitialized=true, Name=MOT1]
7[Address=1C4B5A, IsInitialized=true, Name=REM1]
8[Address=1C4AB5, IsInitialized=true, Name=REM2]
9[Address=1C4B53, IsInitialized=true, Name=REM3]
10[Address=14B35A, IsInitialized=true, Name=SEN1]
instMap (MyMap) {SW1=[Address=14B35A, IsInitialized=true, Name=SEN1
], SW2=[Address=14B35A, IsInitialized=true, Name=SEN1
], SW3=[Address=14B35A, IsInitialized=true, Name=SEN1
], SW4=[Address=14B35A, IsInitialized=true, Name=SEN1
], SW5=[Address=14B35A, IsInitialized=true, Name=SEN1
], SW6=[Address=14B35A, IsInitialized=true, Name=SEN1
], MOT1=[Address=14B35A, IsInitialized=true, Name=SEN1
], REM1=[Address=14B35A, IsInitialized=true, Name=SEN1
], REM2=[Address=14B35A, IsInitialized=true, Name=SEN1
], REM3=[Address=14B35A, IsInitialized=true, Name=SEN1
], SEN1=[Address=14B35A, IsInitialized=true, Name=SEN1
]}

Any idea what am I doing wrong?
 

DonManfred

Expert
Licensed User
Longtime User
For i = 0 To list1.Size-1
insteonDev.Initialize
You are missing a DIM for each new insteonDev

DIM a new instance of insteonDev inside your loop

B4X:
 For i = 0 To list1.Size-1
               Dim insteonDev As InsteonDevList
                insteonDev.Initialize

Edit: I see it just now.... You are reusing the same map. Use a new map each time instead of changing the same? I´m not sure what is correct here.

B4X:
 For i = 0 To list1.Size-1
               Dim instmap As Map
                instmap.Initialize

Without seeing your complete code it is hard to find the exact problem.

But the fact that you are reusing the same map/InsteonDevList instance seems to be the problem here.
 
Last edited:
Upvote 0

wes58

Active Member
Licensed User
Longtime User
You are missing a DIM for each new insteonDev

DIM a new instance of insteonDev inside your loop

B4X:
 For i = 0 To list1.Size-1
               Dim insteonDev As InsteonDevList
                insteonDev.Initialize

Edit: I see it just now.... You are reusing the same map. Use a new map each time instead of changing the same? I´m not sure what is correct here.

B4X:
 For i = 0 To list1.Size-1
               Dim instmap As Map
                instmap.Initialize

Without seeing your complete code it is hard to find the exact problem.

But the fact that you are reusing the same map/InsteonDevList instance seems to be the problem here.

You were correct. When I added "Dim insteonDev As InsteonDevList" inside the loop it works fine.
I thought that if I initialise "insteonDev.Initialize" on each iteration, it will be enough since the log showed correct values of insteonDev on each iteration, but it looks like I had to re-dim the insteonDev as you suggested. I still wonder why?
B4X:
0[Address=1FEFCD, IsInitialized=true, Name=SW1]
1[Address=1FEFAF, IsInitialized=true, Name=SW2]
2[Address=1FEF4B, IsInitialized=true, Name=SW3]
3[Address=1FEF65, IsInitialized=true, Name=SW4]
4[Address=1FEF66, IsInitialized=true, Name=SW5]
5[Address=1FEF57, IsInitialized=true, Name=SW6]
6[Address=1FF40C, IsInitialized=true, Name=MOT1]
7[Address=1C4B5A, IsInitialized=true, Name=REM1]
8[Address=1C4AB5, IsInitialized=true, Name=REM2]
9[Address=1C4B53, IsInitialized=true, Name=REM3]
10[Address=14B35A, IsInitialized=true, Name=SEN1]

Thanks for your help.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I still wonder why?
If you are reusing the same like you did before you are NOT creating multiple instances. You are adding the same objectreference to your map. In result you will have multiple contents but all are carrying the "last changes" to the type-instance.
Adding a dim in your loop will create a new instance each time. And THIS instance is added to the map.

See this for more info about referencing by value.
 
Upvote 0
Top