Android Question Cannot parse: False as boolean

Discussion in 'Android Questions' started by solfinker, Jun 25, 2015.

  1. solfinker

    solfinker Member Licensed User

    I get the error with the following code:
    Code:
    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
     
  2. picenainformatica

    picenainformatica Active Member Licensed User

    Wich is the content of tbstate?
    Log (tbstate)
     
  3. RandomCoder

    RandomCoder Well-Known Member Licensed User

    My guess is that you are returning a string and so will need something like...
    Code:
    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: Jun 26, 2015
    wonder and Erel like this.
  4. Cableguy

    Cableguy Expert Licensed User

    try rediming like this:

    Code:
    dim tbState = list2.Get(1as boolean
     
  5. solfinker

    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.
     
  6. Troberg

    Troberg Well-Known Member Licensed User

    If it's false (as opposed to False), you've got a string, not a boolean.
     
    Cableguy likes this.
  7. Cableguy

    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
     
  8. Troberg

    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.
     
  9. wonder

    wonder Expert Licensed User

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

    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
     
  11. wonder

    wonder Expert Licensed User

    I use this method on my code a lot.

    Code:
    Dim myText = "Yeah!" As String
    Dim Yeah = (myText = "Yeah!"As Boolean

    Label1.Text = Yeah
    Output: true
     
  12. solfinker

    solfinker Member Licensed User

    When trying Wonder solution I get the error at compilation time:
    or
    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.
     
  13. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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!
     
    wonder likes this.
  14. solfinker

    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
    Code:
    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
    Code:
    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.
     
    RandomCoder likes this.
Loading...