If(i Mod 2 = 0) doesn't return a meaninful error message

Penko

Active Member
Licensed User
Longtime User
Dear Erel!

I found out that If(i Mod 2) doesn't produce any B4A compiler error, neither does it fail the java compilation. This error occurs when I start my application:

An error occured in sub:******** (java line: xxx). java.lang.RuntimeException: Cannot parse: 0 as boolean.


For the ones like me who didn't understand at first, the correct syntax is: i Mod 2 = 0.

I couldn't understand where do I have booleans in my code so I opened the java file and found the following:
B4X:
if (BA.ObjectToBoolean((_i%2))) {

If I correct the B4A syntax, I get this: (which is pretty much better and valid)
B4X:
if ((_i%2==0)) {

Erel, please check it out as others may encounter it too.

A kind of suggestion, you may also add if(i Mod 2) to default to = 0 )

Hope this report helps.
 

Jost aus Soest

Active Member
Licensed User
Longtime User
Somewhere in the b4a information it's written (IMHO), that the boolean "True" is not the same like "<>0", as we VB-users are used to use.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you write If i Mod 2 Then expects a Boolean. But the result of is a number therefor it raises an error.
Writing If i Mod 2 = 0 Then is OK because i Mod 2 = 0 gives a Boolean as result !

There is no numeric equivalent in B4A for Booleans.

Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…