J jtare Active Member Licensed User Longtime User Apr 16, 2019 #1 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: Code: Copy Dim mymap As map = File.ReadMap(File.DirInternal,"mymap.txt") Log(Not(mymap.Get("a") Or mymap.Get("b"))) 'True Log("false"=False) 'True Log("false"=True) 'False Log("true"=True) '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: Code: Copy mymap.put("a",False) 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 Code: Copy log(mymap.Get("a") = False ) 'False and Code: Copy log("false"=False) 'True Be the same? Both True? But instead I get False in the first one and True in the second one.
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: Code: Copy Dim mymap As map = File.ReadMap(File.DirInternal,"mymap.txt") Log(Not(mymap.Get("a") Or mymap.Get("b"))) 'True Log("false"=False) 'True Log("false"=True) 'False Log("true"=True) '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: Code: Copy mymap.put("a",False) 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 Code: Copy log(mymap.Get("a") = False ) 'False and Code: Copy log("false"=False) 'True Be the same? Both True? But instead I get False in the first one and True in the second one.
DonManfred Expert Licensed User Longtime User Apr 16, 2019 #2 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 Upvote 0
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
J jtare Active Member Licensed User Longtime User Apr 16, 2019 #3 DonManfred said: You should use B4ASerialisator or Randomaccessfile WriteB4XObject/ReadB4XObject Click to expand... Thanks, I'll give it a try. But still shoudn't Code: Copy log(mymap.Get("a") = False ) 'False and Code: Copy log("false"=False) 'True Be the same? Upvote 0
DonManfred said: You should use B4ASerialisator or Randomaccessfile WriteB4XObject/ReadB4XObject Click to expand... Thanks, I'll give it a try. But still shoudn't Code: Copy log(mymap.Get("a") = False ) 'False and Code: Copy log("false"=False) 'True Be the same?
DonManfred Expert Licensed User Longtime User Apr 16, 2019 #4 Cast the string to a Boolean and compare the two booleans. Upvote 0
Computersmith64 Well-Known Member Licensed User Longtime User Apr 16, 2019 #5 I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function: Code: Copy Private Sub CBool(sVal As String) As Boolean If sVal = "true" Then Return True Else Return False End Sub - Colin. Upvote 0
I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function: Code: Copy Private Sub CBool(sVal As String) As Boolean If sVal = "true" Then Return True Else Return False End Sub - Colin.
J jtare Active Member Licensed User Longtime User Apr 16, 2019 #6 Computersmith64 said: I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function: Code: Copy Private Sub CBool(sVal As String) As Boolean If sVal = "true" Then Return True Else Return False End Sub - Colin. Click to expand... Thanks for the suggestion. I think I will go with this solution for now. Upvote 0
Computersmith64 said: I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function: Code: Copy Private Sub CBool(sVal As String) As Boolean If sVal = "true" Then Return True Else Return False End Sub - Colin. Click to expand... Thanks for the suggestion. I think I will go with this solution for now.
R RB Smissaert Well-Known Member Licensed User Longtime User Apr 16, 2019 #7 Computersmith64 said: I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function: Code: Copy Private Sub CBool(sVal As String) As Boolean If sVal = "true" Then Return True Else Return False End Sub - Colin. Click to expand... Not sure it is any better, but I would do: Code: Copy Return sVal = "true" or maybe better Code: Copy Return sVal.ToLowerCase = "true" RBS Upvote 0
Computersmith64 said: I use ReadMap & WriteMap a lot in my apps to save settings, so for Booleans I just use this utility function: Code: Copy Private Sub CBool(sVal As String) As Boolean If sVal = "true" Then Return True Else Return False End Sub - Colin. Click to expand... Not sure it is any better, but I would do: Code: Copy Return sVal = "true" or maybe better Code: Copy Return sVal.ToLowerCase = "true" RBS
klaus Expert Licensed User Longtime User Apr 16, 2019 #8 Code: Copy Private Mapa, Mapb As Boolean Mapa = MyMap.Get("a") Mapb = MyMap.Get("b") 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. Upvote 0
Code: Copy Private Mapa, Mapb As Boolean Mapa = MyMap.Get("a") Mapb = MyMap.Get("b") 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.
Cableguy Expert Licensed User Longtime User Apr 16, 2019 #9 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? Upvote 0
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?
J jtare Active Member Licensed User Longtime User Apr 16, 2019 #10 klaus said: Code: Copy Private Mapa, Mapb As Boolean Mapa = MyMap.Get("a") Mapb = MyMap.Get("b") 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. Click to expand... Thanks. Cableguy said: 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? Click to expand... 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. Upvote 0
klaus said: Code: Copy Private Mapa, Mapb As Boolean Mapa = MyMap.Get("a") Mapb = MyMap.Get("b") 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. Click to expand... Thanks. Cableguy said: 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? Click to expand... 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.