Android Question Map1.Put with two separate values

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Hi there,

is there a way to set more than one "value"?

something like this:
B4X:
 Map1.Put("Key1", "Value1", "Value2")

BR
Sinan

PS: Unfortunately, I can't do something like that, because the first Value1 contains a string and this character (|) may also appear there. And then I'd have the problem with splitting
B4X:
Map1.Put(Key1, Value1 & "|" & Value2)
 

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Another option is to create a custom type:
B4X:
Type MyValue (Value1 As String, Value2 As String)
'now hover over MyValue and choose "generate create type sub"

Map1.Put("Key", CreateMyValue("Value1", "Value2"))

How can I GET ?
B4X:
Map1.Get ??
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
How can I GET ?


B4X:
Type typPerson(ID As Int, Name As String, Age As Int)


Dim Persons As Map
Persons.Initialize

Dim ID As Int = 4
Dim Person As typPerson = CreatetypPerson(ID, "Erel", 32)
Persons.Put(ID, Person)


Log(Persons.Get(4).As(typPerson).Name)


Public Sub CreatetypPerson (ID As Int, Name As String, Age As Int) As typPerson
    Dim t1 As typPerson
    t1.Initialize
    t1.ID = ID
    t1.Name = Name
    t1.Age = Age
    Return t1
End Sub



Tip:

1630154222231.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But you have to be careful!
You must not save the map as a text file, because after reading the content of "map1.Get("Key")" is only pure text.
That means that the code "Dim xvalue As MyValue = map1.Get("Key")" does not work anymore.
That's true. You should use KeyValueStore or B4XSerializator instead of File.WriteMap.
 
Upvote 0
D

Deleted member 103

Guest
Is it possible to reopen what was saved with this procedure,
B4X:
Public Sub WriteMapAsB4XObject(sDir As String, sFilename As String, mp As Map)
    Dim raf As RandomAccessFile
    raf.Initialize2(sDir, sFilename, False, True)
    raf.WriteB4XObject(mp, raf.CurrentPosition)
    raf.Close
End Sub
and read again with this ?
B4X:
Dim m as Map = ser.ConvertBytesToObject(File.ReadBytes(...))

I ask because then I could replace my 4 procedures with a single one.
 
Upvote 0
Top