Android Question getting/putting booleans in maps - must cast to boolean?

Dave O

Well-Known Member
Licensed User
Longtime User
Hi all,

I often use maps (with readMap and writeMap) to load and save data.

However, I just noticed that if I save a boolean value to a map:
B4X:
settingsMap.Put("sound", True)

...and load it later with:
B4X:
settingsMap.Get("sound")

...it appears to work:
B4X:
Log("sound = " & settingsMap.Get("sound"))  'returns "sound = true"

...but when I test it, it fails:
B4X:
If settingsMap.Get("sound") = True Then        'returns false

It only seems to work if I explicitly cast it to a boolean:
B4X:
If settingsMap.Get("sound").As(Boolean) = True Then        'returns true as intended

I found this confusing, thinking at first that there must be implicit casting happening. The "put" seemed to implicitly cast True to "true", and the "get" returned "true" (no surprise) but the expression didn't implicitly cast "true" to True.

I wonder if this is one of those where I should write the known type first in the expression?

B4X:
If True = settingsMap.Get("sound") Then

I'm guessing this may trip up others, so I hope this helps.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
...but when I test it, it fails
it is expected as the loaded value IS a STRING



Use B4XEncryption library and

B4X:
    Dim ser As B4XSerializator
   
    Dim m As Map
    m.Initialize
    m.Put("Bool",True)
    Dim bytes() As Byte = ser.ConvertObjectToBytes(m)
    File.WriteBytes(File.DirInternal,"data.map",bytes)

    Dim bytesnew() As Byte = File.ReadBytes(File.DirInternal,"data.map")
   
    Dim mapnew As Map = ser.ConvertBytesToObject(bytesnew)
    If mapnew.Get("Bool") = True Then
        Log($"TRUE :D"$)
    Else
        Log($"Dammit..."$)
    End If
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…