Converting Booleans to Integers

Is it dangerous to do the following?

Dim i As Integer = CType(False, Int32) // i is now 0 Dim j As Integer = CType(True, Int32) // j is now 1
The documentation on CType() doesn’t talk about this, but it also doesn’t explicitly forbidden it (like for Boolean -> String and String -> Boolean).

That sounds perfectly okay.
Booleans are a kind of integer internally.

I don’t think it is a problem but you do need to be careful not to take the analogy too far. While it is true that FALSE is equivalent to the value zero, it isn’t always true that TRUE is the equivalent of the value one. Technically while FALSE is zero, TRUE is NOT ZERO.

But then, I was an operating systems coding geek.

Well, and sometimes you run into the problems.
In C people often write stuff like: if (value & 8). This means that the actual boolean value processed it 0 or 8. And if you assign that to a boolean property, it’s valid of course. Just later if you check for = 1,it will fail.

Right, Dale and Christian, that’s why I always cringe when I see “if mybool = true then”. It may be safe in Xojo because Booleans really can’t have any other values than false and true, but I see the same in C code, where this is just unsafe and wrong. shudder

The problem can be if the plugin returns a C boolean which is not 0 or 1.

For a plugin designed for use with RS/Xojo, then surely that would count as a bug in the plugin.

Yes, so we fixed it.

I was sure you would have ! – but it’s a fair warning to look out for when using plugins from any other source ( not that I need to so far ).

FYI, in C, to convert an int to a boolean, one proper way is:

int x = ...; return x != 0;