And how they work …..
In a bug report I made I posted a tiny sample of code
Dim tokens() As String
Select Case True
Case tokens.ubound < 0
Case tokens(0) = "End"
Case "Dim"
break
End Select
(Not this code has a known bug in it – Case “Dim” is wrong but it illustrated the bug I was reporting) I’ve used this style for very long sections of code esp where I dont want to clutter it up with extraneous “if then” or “elseif”
But it gathered a curious comment that convinced me the writer of that comment isnt certain how SELECT CASE works. The comment was
I see no sense in the line „Select Case True“, since True is always True. So the error message points in the right direction, correcting this line.
While I understand the reasoning it also happens to illustrate the misunderstanding of how select case works.
The statement that follows select case is known as the Test Expression.
Xojo will then evaluate each case expression and find the first one that matches that Test Expression.
In the case of the code above it will find the first one that has a case expression that is true
It behaves as if the code was written as
if true = (tokens.ubound < 0) then
elseif true (tokens(0) = "End") then
elseif true = ( "Dim" ) then
end
This sample replicates the same bug as the original BUT this time the compiler correctly complains about the comparison between TRUE and “DIM” (which is what my bug report was about originally)
Select case can be a very handy way to declutter code and remove a lot of “if then” and “elseif then” from your code.
But only if you know how it works.