Android Question B4XOrderedMap / Map - putting a type object problem

DroidLyon

Member
Licensed User
Longtime User
Hi people
I've used the map collection storing "Type Object" values many times without issue and then hit a problem yesterday trying to store(put) a Type object into a B4XOrderedMap. It also fails with a standard Map... Hence I suspect a dumbass programming error :confused:

So I declare a type, dim a type object, and then use a for loop to put data into the type object and then store(put) into a B4xOrderedmap or standard map
If I use the type object it (possibly) puts only the last Type object created, If I try and store a string it works fine ?!

Code below, Simple Project using (B4a Core 9.00, B4xCollections 1.05) which simply logs output from starter service.
Apologies for using up your time, but I have been "tearing my hair out" since yesterday to get my head round this. I suspect I may have to hang my head in embarrassment ?:oops:
Thanks in advance

All code in Starter service..

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Type tData1(tdID As Int,tdCity As String,tdMonth As Int)

B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
   
    Dim td1 As tData1
    td1.Initialize
    Dim omData1     As B4XOrderedMap = B4XCollections.CreateOrderedMap()
   
    For i = 0 To 12
           
            Log("icount = " & i)
            td1.tdID = i
            td1.tdCity = "City" & i  
            td1.tdMonth = i
            Log("put data for " & i & "  td id " & td1.tdID)  
           
            omData1.Put(i,td1 )
   
        '    omData1.Put(i,"test" & i )  ' This successfully stores and with loop below logs "omdata 1 test1"  "omdata 1 test2" etc
        '    td1.Initialize    ' this stores ID's of 0 ?
    Next
   
   
    Log("mdata1 size is " & omData1.Size)

    For Each c As Int In omData1.Keys
        Log("omdata " & c & "   "  & omData1.Get(c))
    Next

Log gives..

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
icount = 0
put data for 0  td id 0
icount = 1
put data for 1  td id 1
icount = 2
put data for 2  td id 2
icount = 3
put data for 3  td id 3
icount = 4
put data for 4  td id 4
icount = 5
put data for 5  td id 5
icount = 6
put data for 6  td id 6
icount = 7
put data for 7  td id 7
icount = 8
put data for 8  td id 8
icount = 9
put data for 9  td id 9
icount = 10
put data for 10  td id 10
icount = 11
put data for 11  td id 11
icount = 12
put data for 12  td id 12
omdata1 size is 13
omdata 0   [IsInitialized=true, tdCity=City12, tdID=12
, tdMonth=12]
omdata 1   [IsInitialized=true, tdCity=City12, tdID=12
, tdMonth=12]
omdata 2   [IsInitialized=true, tdCity=City12, tdID=12
, tdMonth=12]
omdata 3   [IsInitialized=true, tdCity=City12, tdID=12
, tdMonth=12]
etc, etc
 

Attachments

  • B4xOrderedMap3Test.zip
    8.5 KB · Views: 338
  • B4xMAPTest2.zip
    8.4 KB · Views: 347

josejad

Expert
Licensed User
Longtime User
Hi:

Im not in my computer now so I can’t test, but have you tried to initialize the td1 into the loop? Or even Dim it into the loop (I think this is the proper way)
 
Upvote 0

DroidLyon

Member
Licensed User
Longtime User
Hi:

Im not in my computer now so I can’t test, but have you tried to initialize the td1 into the loop? Or even Dim it into the loop (I think this is the proper way)

Hi José

It worked !! So muchas gracias :)

As you can see from code in original post, I have commented out a "td1.initialise" - which stored ID's of 0 and I remember "randomly " initialising the td1 and the omdata1 in various places including in globals but missed this one..

The code below works and produces log output correctly
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
   
'    Dim td1 As tData1
'    td1.Initialize
    Dim omData1     As B4XOrderedMap = B4XCollections.CreateOrderedMap()
   
    For i = 0 To 12
       
            Dim td1 As tData1
            td1.Initialize
            Log("icount = " & i)
            td1.tdID = i
            td1.tdCity = "City" & i  
            td1.tdMonth = i
            Log("put data for " & i & "  td id " & td1.tdID)  
           
            omData1.Put(i,td1 )
   
        '    omData1.Put(i,"test" & i )  ' This successfully stores and with loop below logs "omdata 1 test1"  "omdata 1 test2" etc
        '    td1.Initialize    ' this stores ID's of 0 ?
    Next

B4X:
icount = 9
put data for 9  td id 9
icount = 10
put data for 10  td id 10
icount = 11
put data for 11  td id 11
icount = 12
put data for 12  td id 12
mdata1 size is 13
omdata 0   [IsInitialized=true, tdCity=City0, tdID=0
, tdMonth=0]
omdata 1   [IsInitialized=true, tdCity=City1, tdID=1
, tdMonth=1]
omdata 2   [IsInitialized=true, tdCity=City2, tdID=2
, tdMonth=2]
omdata 3   [IsInitialized=true, tdCity=City3, tdID=3
, tdMonth=3]


I understand the need for initialisation, and I understand the scope of variables can be global or private (etc), but within a sub routine I'd just assumed that the object was available throughout and whether it was in a sub routines loop or out was largely not important
I just assumed that once initialised it was some kind of fixed object to which the loop would simply overwrite prevous values and hence be re-used to write to the ordered mapmap.

If you or anyone could explain further why it works when initialisation is within the loop rather than outside it, I'd be very grateful ?!

Thanks again for looking José
DL
 
Upvote 0
Top