Here is a piece of code that makes me think to a Xojo bug:
if not Listbox1.CellType(0,0) = Listbox.TypeCheckbox then
msgbox "is NOT a checkBox"
else
msgBox "IS a checkBox"
end if
The above code tells me a listbox cell IS a checkbox, while its not.
OK, I know this can be better tested with if Listbox1.CellType(0,0) <> Listbox.TypeCheckbox and I know that the NOT operator has an higher precedence over the = operator.
But here, seems Xojo is first negating the Listbox1.CellType(0,0) and then comparing with Listbox.TypeCheckbox.
This would be fine, stated that NOT has an higher precedence, but still not possible, since the Listbox.CellType method returns an integer.
Indeed, writing
if not Listbox1.CellType(0,0) then
I get a Type Mismatch Error.
So in the end, while the first code compile with no errors?
The OP example is applying a BITSWISE “NOT”, while the example with the statement in Parends is executing a proper LOGICAL NOT, they are quite different and as observed produced very different results, but both for a proper reason.
[quote=62363:@Massimo Valle] I know that the NOT operator has an higher precedence over the = operator.
[/quote]
Apparently you overlooked this thing you know
Since NOT has higher precedence you HAVE to use () to force the evaluation to be what you intend
if not ( Listbox1.CellType(0,0) = Listbox.TypeCheckbox ) then
msgbox "is NOT a checkBox"
NOT a bug - but certainly a confusing thing if you’re not aware of the dual nature of NOT
When I did VB I liked the dual nature of AND and OR and (less often) NOT. When I went to REAL that wasn’t available so I got used to the BitwiseAnd etc. methods. Now, given the opportunity to go back, I won’t. Now, I won’t even come close to putting AND/OR/NOT even close to non-boolean things. And I never use NOT if I can help it, I just rewrite it check the TRUE, not the FALSE.
I also learned the same lesson with operator precedence. Given the good advice of “don’t bother to remember” (Code Complete, Steve McConnell) I use parentheses completely - no compound statements ever.
After years of trying to understand code I wrote long ago, I insist I write things in a self documenting manner - never be assumptive, ALWAYS be explicit.
Isn’t ambiguity a wonderful thing? A long time ago a programming teacher told me that Murphy’s Law applies to coding. Essentially, if a line of code CAN be misinterpreted, it WILL be misinterpreted. He stressed the idea, as Norman said, use bracketing to make sure the code will do what you think you want it to do.
I completely agree on this. I always put bracket even if not necessary, even just because this permits me to set crystal clear my intentions. And this is even more needed in a language like Xojo, where many “automatisms” are in place, like implicit type conversions. The whole discussion started from a piece of code I found and that puzzled me for the results.
Code from others is always interesting. You learn a lot, in what you can do, and in what you shouldn’t.