Android Question Types can't be saved in maps?

Scantech

Well-Known Member
Licensed User
Longtime User
I am interested in assigning type data in map object. See attached project which clearly shows maps will have same values. Any data type added to maps will be replaced with latter data. 2 sizes of map with different data added, but shows same data result?
 

Attachments

  • maptest.zip
    10.1 KB · Views: 37

William Lancee

Well-Known Member
Licensed User
Longtime User
Since you did not Dim and Initialize a second instance of GetDataType then changes you made are to the the original instance.
Both First Data Type and Second Data Type now point to the same changed object.

You can reuse a name but you have to associate it with a new object.

Note: if you hover your mouse over Type DataType, you'll be given a choice to generate a createDatatype Sub.
In your case just delete the parameters and initialize the maps and lists like I did below.


B4X:
Private Sub Button1_Click
    Dim mContainer As Map
    mContainer.Initialize
   
    GetDataType = CreateDataType
    GetDataType.Map1.Put("Index0", "Value0")
    GetDataType.Map2.Put("Index0b", "Value0b")
    GetDataType.List1.Add("List0")
    GetDataType.List2.Add("List0b")

    mContainer.Put("First Data Type", GetDataType)
   
    '-----------------------------------------------

    GetDataType = CreateDataType
    GetDataType.Map1.Put("Index1", "Value1")
    GetDataType.Map2.Put("Index1b", "Value1b")
    GetDataType.List1.Add("List1")
    GetDataType.List2.Add("List1b")
    mContainer.Put("Second Data Type", GetDataType)

    '------------------------------------------------
   
    'Read the container
    Log(mContainer.Get("First Data Type").As(DataType).Map1)     'Log: (MyMap) {Index0=Value0}
    Log(mContainer.Get("Second Data Type").As(DataType).Map1)    'Log: (MyMap) {Index1=Value1}

End Sub

Public Sub CreateDataType As DataType
    Dim t1 As DataType
    t1.Initialize
    t1.Map1.Initialize
    t1.List1.Initialize
    t1.Map2.Initialize
    t1.List2.Initialize
    Return t1
End Sub
 
Last edited:
Upvote 0
Top