Android Question "true","false",True, False How do they work?

Discussion in 'Android Questions' started by jtare, Apr 16, 2019.

  1. jtare

    jtare Active Member Licensed User

    I noticed that for some reason a boolean stored in a map that was read from a file no longer is a boolean but a string maybe?

    I ran some tests where mymap.get("a") is False and mymap.get("b") is False and got this:
    Dim mymap As map = File.ReadMap(File.DirInternal,"mymap.txt")
    Log(Not(mymap.Get("a"Or mymap.Get("b"))) 'True
        Log(mymap.Get("a") = False And mymap.Get("b") = False'False
        Log(mymap.Get("a") = False'False
        Log(mymap.Get("b") = False'False
        Log(mymap.Get("a") = "false" And mymap.Get("b") = "false"'True
        Log(mymap.Get("a") = "false"'True
        Log(mymap.Get("b") = "false"'True
    Both "a" and "b" value are set with a boolean variable, not string. Like this:
    I used to store the map in the keyvaluestore database and worked as expected. This weird behavior started when I moved to save it and read it from a file.

    If the mymap is no longer holding a boolean but a string, shouldn't
    log(mymap.Get("a") = False ) 'False
    Be the same? Both True? But instead I get False in the first one and True in the second one.
  2. DonManfred

    DonManfred Expert Licensed User

    [​IMG] File. WriteMap (Dir As String, FileName As String, Map As Map)
    Creates a new file and writes the given map. Each key value pair is written as a single line.
    All values are converted to strings. <--- !!!

    You should use B4ASerialisator or Randomaccessfile WriteB4XObject/ReadB4XObject
    José J. Aguilar and jtare like this.
  3. jtare

    jtare Active Member Licensed User

    Thanks, I'll give it a try.

    But still shoudn't
    log(mymap.Get("a") = False ) 'False
    Be the same?
  4. DonManfred

    DonManfred Expert Licensed User

    Cast the string to a Boolean and compare the two booleans.
    jtare likes this.
  5. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function:

    Private Sub CBool(sVal As StringAs Boolean
    If sVal = "true" Then Return True Else Return False
    End Sub
    - Colin.
    mangojack and José J. Aguilar like this.
  6. jtare

    jtare Active Member Licensed User

    Thanks for the suggestion. I think I will go with this solution for now.
  7. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Not sure it is any better, but I would do:

    Return sVal = "true"
    or maybe better

    Return sVal.ToLowerCase = "true"
    jtare likes this.
  8. klaus

    klaus Expert Licensed User

    Private Mapa, Mapb As Boolean
    Mapa = MyMap.Get(
    Mapb = MyMap.Get(
    This works.
    The problem is that MyMap.Get("a") is a String.
    But, if you set MyMap.Get("a") to a Boolean variable, like Mapa = MyMap.Get("a"), B4A sets the value correctly.
    jtare likes this.
  9. Cableguy

    Cableguy Expert Licensed User

    Why not "simplify" things and set True=1 and false = 0, the IDE would then convert the string to the correct type, would it not?
    vecino and jtare like this.
  10. jtare

    jtare Active Member Licensed User


    Will have to check if "0"=0 is the same to mymap.get("a")=0, if not it kind of leave me in the same situation.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice