Android Question basic doubt about accessing and change Map objects

giggetto71

Active Member
Licensed User
Longtime User
Hi all,
say I have Map made of objects of a custom type.

B4X:
Type MyObjType (Field1 As String,Field2 as string)
Dim MyMap as Map
.....
Dim MyElement As MyObjType
MyElem.Initialize
MyElem.Field1 = "whatever1"
MyElem.Field2 = "whatever2"
MyMap.Put("key1",MyElem)

MyElem.Initialize
MyElem.Field1 = "whatever3"
MyElem.Field2 = "whatever4"
MyMap.Put("key2",MyElem)
.....

Now based on sample above I would like to be able to do somehting like:

B4X:
MyMap("Key1").Field1 = "SomethingDifferent"

I know that I could just make a "put" with "Key1" but I have noticed that in that case the object is put at the "end" of the map.I know that maps are not made to have an order but I woud save a lot of extra code if that was possible (it is possible in VB for example..)
thanks
 

William Lancee

Well-Known Member
Licensed User
Longtime User
A map can have any object so the type has to be specified in order to recognize "Field1" as a property.
The following works.

B4X:
Dim MyMap As Map: MyMap.Initialize
Dim MyElem1 As MyObjType: MyElem1.Initialize
MyElem1.Field1 = "whatever1"
MyElem1.Field2 = "whatever2"
MyMap.Put("key1",MyElem1)

Dim MyElem2 As MyObjType: MyElem2.Initialize
MyElem2.Field1 = "whatever3"
MyElem2.Field2 = "whatever4"
MyMap.Put("key2",MyElem2)

Dim temp As MyObjType = MyMap.Get("key1")
temp.Field1 = "SomethingDifferent"

Dim myOb As MyObjType  = MyMap.Get("key1")
Log(myOb.Field1)
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
FYI

@Erel says about B4XOrderedMap:

The order of items is preserved. This is the case with regular Maps in B4A and B4J as well, however not the case with B4i regular Maps.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Again for FYI, when you define a Type in Process_Globals you can hover over it's name and choose to automatically add a creator Sub.

B4X:
Sub Process_Globals
    Type MyObjType(Field1 As String,Field2 As String)        'Hover over MyObjType
End Sub

Public Sub CreateMyObjType (Field1 As String, Field2 As String) As MyObjType
    Dim t1 As MyObjType
    t1.Initialize
    t1.Field1 = Field1
    t1.Field2 = Field2
    Return t1
End Sub

'Then everything is simplified

    Dim MyMap As Map: MyMap.Initialize
    MyMap.Put("key1", CreateMyObjType ("whatever1", "whatever2"))
    MyMap.Put("key2", CreateMyObjType ("whatever3", "whatever4"))
   
    Dim temp As MyObjType = MyMap.Get("key1")
    temp.Field1 = "SomethingDifferent"
   
    Dim myOb As MyObjType  = MyMap.Get("key1")
    Log(myOb.Field1)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
And it installs very quickly (the source code must be very compact!)

For Type, I usually create an updater function, depending on my need.

B4X:
Public Sub UpdateMyObjType (Obj As MyObjType, Field1 As String, Field2 As String)
    If Field1.Length>0 Then Obj.Field1 = Field1
    If Field2.Length>0 Then Obj.Field2 = Field2
End Sub

    Dim MyMap As Map: MyMap.Initialize
    MyMap.Put("key1", CreateMyObjType ("whatever1", "whatever2"))
    MyMap.Put("key2", CreateMyObjType ("whatever3", "whatever4"))
    UpdateMyObjType (MyMap.Get("key1"), "SomethingDifferent", "")
 
Upvote 0
Top