B4J Question Proper use of Boolean expressions in If statements [solved]

Didier9

Active Member
Licensed User
It seems that the handling of "If <boolean expression> then" is not handled consistently. Actually my observations were made under B4A but I see that form used in B4J code examples as well.

It seems that sometimes "If <boolean expression> then" does not work while "If <boolean expression> = True Then" does work, so I have gotten in the habit of always adding "= True" in the If statements. In the original Microsoft Visual Basic 6.0 (which is my background), the first form always works as long as the expression can be converted into a boolean form. Visual Basic will go out of its way to do sometimes unexpected type conversions without as much as a warning.

Looking at the code for B4JPackager, I do see statements like "If Windows Then" where Windows is a Boolean type.

So, what are the rules?
 

Didier9

Active Member
Licensed User
I do not have any handy at the moment, as I said I have been avoiding that construct for some time.
The question really is: what are the rules?

I ran some test. It looks like the tested variable has to be or evaluate to a boolean. It does not work if the variable is an integer, even if the integer contains the value 0 or 1.
That's what confused me because if you run "Dim i as integer: i=0: if i then" Visual Basic will gladly cast the integer into a boolean before doing the test and not complain. B4X does compile but complains (at run time) if the variable is not a boolean type, regardless of the value.

Example: This code:
B4X:
Dim i As Int
   
    i = 0
    If i Then
        Log("True")
    Else
        Log("False")
    End If
compiles but produces this error at run time:
B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 18 (Main)
java.lang.RuntimeException: Cannot parse: 0 as boolean
    at anywheresoftware.b4a.BA.parseBoolean(BA.java:341)
    at anywheresoftware.b4a.BA.ObjectToBoolean(BA.java:410)
    at b4j.example.main._appstart(main.java:94)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at b4j.example.main.start(main.java:38)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
where Visual Basic with similar code will run with mostly the expected result.

Sometimes the similarity between Visual Basic and B4X, while generally helpful, can be confusing.
 

MarkusR

Well-Known Member
Licensed User
other languages interpret an integer =0=false and <>0 as true
in your case u must write (i<>0) or make a sub Boolean(value as integer) as Boolean

but i agree the compiler can interpret
If i Then as if i have a value

otherwise i not like automatic type convert. if indeed optional.

this error "Cannot parse: 0 as boolean" should not be at runtime, instead at compile time with incorrect condition.
 
Last edited:
Top