Conditional check failing for a Boolean type

agraham

Expert
Licensed User
Longtime User
Am I missing something obvious as this small program doesn't work as I expect? The conditional test fails and the msgbox isn't shown. It fails in the IDE and both compilers.

B4X:
Sub Globals
  'Declare the global variables here.
  Dim Checks(10) As Boolean
  Dim Flag
End Sub

Sub App_Start
  For x = 0 To 9
    Checks(x) = false
  Next
  MsgBox(Checks(0))   
  Form1.Show
  Test
End Sub

Sub test
  'If Not(Checks(0)) Then ' this works
  If Checks(0) = false Then ' this doesn't when Checks() are Boolean
    Msgbox("False")
  End If
End Sub
 

colin9876

Active Member
Licensed User
8 bits not 1

I came accross this a while back, from memory I think boleans are implemented in Basic4ppc as 8 bits (not 1 bit as it strictly should be) so perhaps the check is comparing 0000001=1 or something like that?
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi agraham,
I confirm your finding. This phenomena shows up only with user
defined Boolean flags. There is an earlier discussion on this issue:
http://www.b4x.com/forum/showthread.php?t=1358

If flag = true Then...NG
If flag Then...OK

If flag = false Then...NG
If Not(flag) Then...OK
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should not declare the array as a boolean type.
Change it to:
B4X:
Sub Globals
  'Declare the global variables here.
  Dim Checks(10)
  Dim Flag
End Sub

Sub App_Start
  For x = 0 To 9
    Checks(x) = false
  Next
  MsgBox(Checks(0))    
  Form1.Show
  Test
End Sub

Sub test
  'If Not(Checks(0)) Then ' this works
  If Checks(0) = false Then ' this doesn't when Checks() are Boolean
    Msgbox("False")
  End If
End Sub
 

agraham

Expert
Licensed User
Longtime User
Hi Robert
There is an earlier discussion on this issue:
http://www.b4x.com/forum/showthread.php?t=1358
This is actually a different issue - that was boolean treatment in B4PPC default types. This is a "problem" with actual Boolean types.

Hi Erel
You should not declare the array as a boolean type.
Change it to
That's a bit of get-out! Of course I know that works with default types but I wanted a large array to pass to a library and thought that I would declare it Boolean to save memory. I ran into this problem manipulating the returned results when I wrote what "came naturally" as a VB type conditional. At least the "C" type "if Bool then" conditional check works.
 

colin9876

Active Member
Licensed User
hmmm

Have to say I think if an option to declare as Bolean is there it should work 'as expected'
its potentially problematic to have to keep a mental list of what ways things should and shouldnt be done? Especially for newbies or people porting basic code from other sources??

Im confused so the exact definitions in Basic4ppc of
true
True
false
False

Can anyone tell me?
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Im confused so the exact definitions in Basic4ppc of
true
True
false
False
Can anyone tell me?

true and True are the same as are False and false.
"true" and "True" are not the same, neither are "false" and "False"

var = True actually sets a variable to the string value "true"
var = False actually sets a variable to the string value "false"
Because they are not quoted strings but are reserved keywords B4PPC treats them as a special case and lower-cases them if they are not already.

var = "true" achieves the same as var = true but var = "True" is NOT the same as in this case "True" is a quoted string and is assigned unchanged.

B4PPC conditional equality checks for true and false appear to be actually string comparisons against "true" and "false".

All this is because of the weak typing implemented in B4PPC.
 

colin9876

Active Member
Licensed User
hmmm some typing might be useful

Thanks ... clear as mud lol!

Erel - I mentioned once before that personally I would like to have the option to 'type' a variable, for example number% as INT.
It would give speed increases, as Ive already found using number(0) as INT to perform faster than number when it defaults to a string.

Would this be something u would consider for the next version of Basic4ppc or do u not think its a good idea?
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Andrew,
Thanks a lot for the lecture. That ties it. As a former embVB programmer I
imagined True = -1 and False = 0, irrespective of case. I should have
realised that True and False are no Basic4ppc constants like cPI, cE, etc.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…