Boolean problem

agraham

Expert
Licensed User
Longtime User
In the IDE this code does not error but does not behave correctly when ANDing a Boolean with another Boolean or a True or False literal value. It immediately exits the IF block without executing the Msgbox statement. It does throw an error if you try to compile. OR doesn't work either but NOT() seems to be OK

B4X:
Sub test
   Dim b As Boolean
   If b = False Then
      b = b AND True
      Msgbox(b)
   End If
End Sub
 

mjcoon

Well-Known Member
Licensed User
Possibly unrelated, but...

I have been "getting away with" having a Sub declared "As Boolean" (dating back to when I thought that was valid).

But when I tried to compile with the new beta version I got a complaint "cannot implicitly convert type 'string' to 'bool'" (as attached). The Sub has a number of Return statements which all use "False" or "True" values, as you might expect. The error message points to the last line of the Sub.

I had wondered if the built-in constants have implied types in the new language.

Mike.
 

Attachments

  • coerce problem.jpg
    coerce problem.jpg
    14.6 KB · Views: 144

agraham

Expert
Licensed User
Longtime User
Are you using the latest 6.851? I've tried Subs that return Booleans and it works for both variables and False and True keywords and "False" and "True" as string literals. Even the default return value from End Sub properly returns false.

What does your Sub declaration look like. It looks like the Sub is returning a string value.
 
Last edited:

mjcoon

Well-Known Member
Licensed User
Are you using the latest 6.851? I've tried Subs that return Booleans and it works for both variables and False and True keywords and "False" and "True" as string literals. Even the default return value from End Sub properly returns false.

What does your Sub declaration look like. It looks like the Sub is returning a string value.

Yes, I'm running (it says) 6.851.

B4X:
Sub openFile(activity, inOut, saveFile) As Boolean

Immediately before the End Sub there is an ErrorLabel(openFileErrHandler) portion which also has its own Return False.

Mike.
 

mjcoon

Well-Known Member
Licensed User
The problem is caused by the Errorlabel statement which emits a code structure with a default return of an empty string at End Sub instead of a Boolean.

Thanks Andrew, I guess there isn't going to be a work-around for that (other than omitting the "As..") without a fix from Erel.

Mike.
 
Top