Android Question Cannot parse: False as boolean

solfinker

Member
Licensed User
I get the error with the following code:
B4X:
Sub Process_Globals
    Dim tbState As Boolean
End Sub
Sub Globals
    Dim list2 As List
    Private ToggleButton1 As ToggleButton
End Sub
Sub Activity_Create(FirstTime As Boolean)
list2 = File.ReadList(File.DirRootExternal, "cs/parameters.txt")
tbState = list2.Get(1)
ToggleButton1.Checked = tbState
End Sub
Thank you for your help
 

RandomCoder

Well-Known Member
Licensed User
My guess is that you are returning a string and so will need something like...
B4X:
Sub Activity_Create(FirstTime As Boolean)
list2 = File.ReadList(File.DirRootExternal, "cs/parameters.txt")
Dim strBool As String = list2.Get(1)
If strBool.ToLowerCase = "true" Then
  tbState = True
Else
  tbState = False
End If
ToggleButton1.Checked = tbState
End Sub
 
Last edited:

solfinker

Member
Licensed User
1. The content - prior to error - of tbState is False

2. rediming does not help

3. Changing the code according to RC helps - the error disappears - but the value of tbState is neither False nor True, but false. So it must be false - or true - and not False the right value for a Boolean.

Thank you very much for your help.
 

Cableguy

Expert
Licensed User
Another option is to use the 1/0 approach.
In your config file use 1 for true and 0 for false, then use something similar to RC's code to check its value. B4a handles numbers independently of if they are strings or integers

n="0" is the same as n=0 for most operations
 

Troberg

Well-Known Member
Licensed User
Or, explicitly convert them to known strings (I usually use "Yes" and "No", to make them more readable to a user, and read them case insensitively) when storing and reading them.
 

wonder

Expert
Licensed User
All you have to do is to change this single line of code from this:
B4X:
tbState = list2.Get(1)
Into this:
B4X:
tbState = (list2.Get(1).ToLower = "true")
@RandomCoder, this is the same as your solution, but without the need of extra variables or IF statements. :)
 

sorex

Expert
Licensed User
not sure if .checked will take "true" tho, that's why he added the if to make it a valid true value
 

solfinker

Member
Licensed User
When trying Wonder solution I get the error at compilation time:
Are you missing a library reference?
Occurred on line: 43
tbState = (list2.Get(1).ToLowerCase.true)
Word: tolowercase
or
Are you missing a library reference?
Occurred on line: 43
tbState = (list2.Get(1).ToLowerCase="true")
Word: tolowercase
so I am keeping the IF statements.

Of course I am reading to list2 a string - "False" or "True". I suppose I must have used the 1/0 approach.

Thank you very much for your help.
 

RandomCoder

Well-Known Member
Licensed User
3. Changing the code according to RC helps - the error disappears - but the value of tbState is neither False nor True, but false. So it must be false - or true - and not False the right value for a Boolean.
I must admit that this makes little sense to me? The reason I selected to use ToLowerCase is because I didn't know how you had stored the values in the text file which could have been any of the following "True", "TRUE", and "true". By converting to lower case I guaranteed that they would all be evaluated to "true" regardless so how they were saved.
If all your values are returning False then I can only imagine that you are not returning the word true from the text file. Is it possible that you have a space in there i.e. " true" or "true " in which case the comparison will always return false.

Sorry for the late reply, I hope that you have sorted the problem by now.

Ps @wonder that's a very tidy solution you proposed!
 

solfinker

Member
Licensed User
Thank you RC.
All my values are returning False?
The file I read - according to NotePad++ contains either {5,True} or {5,False}
so after
B4X:
Sub Process_Globals
   Dim tbState As Boolean 
End Sub
Sub Activity_Create(FirstTime As Boolean)
list2 = File.ReadList(File.DirRootExternal, "cs/parameters.txt")
Dim strBool As String = list2.Get(1)
strBool equals "False" or "True".
So far so good.
After
B4X:
If strBool.ToLowerCase = "true" Then
      tbState = True
    Else
      tbState = False
    End If
tbState is either "false" or "true" which makes no sense unless - as I suggested before - it must be false - or true - and not False or True the right value for a Boolean.
The fact is that the file parameters is read by a program running on a pc - vb.net - and it overwrites false or true to False or True.
Anyway, the program is running.
Thank you very much for your help.
 
Top