{"id":794,"date":"2020-07-09T09:35:00","date_gmt":"2020-07-09T15:35:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=794"},"modified":"2020-07-06T09:51:33","modified_gmt":"2020-07-06T15:51:33","slug":"select-case-vs-if-then","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/07\/09\/select-case-vs-if-then\/","title":{"rendered":"Select case vs if then"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">And how they work &#8230;..<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a bug report I made I posted a tiny sample of code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim tokens() As String\n\nSelect Case True\n  \nCase tokens.ubound &lt; 0\n  \nCase tokens(0) = \"End\"\n  \nCase  \"Dim\"\n  break\n  \nEnd Select\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(Not this code has a known bug in it &#8211; Case &#8220;Dim&#8221; is wrong but it illustrated the bug I was reporting) I&#8217;ve used this style for very long sections of code esp where I dont want to clutter it up with extraneous &#8220;if then&#8221; or &#8220;elseif&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But it gathered a curious comment that convinced me the writer of that comment isnt certain how SELECT CASE works. The comment was <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>I see no sense in the line \u201eSelect Case True\u201c, since True is always True. So the error message points in the right direction, correcting this line.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">While I understand the reasoning it also happens to illustrate the misunderstanding of how select case works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The statement that follows <em>select case<\/em> is known as the <em>Test Expression<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Xojo will then evaluate each case expression and find the first one that matches that Test Expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the case of the code above it will find the first one that has a case expression that is true<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It behaves as if the code was written as <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if true = (tokens.ubound &lt; 0) then\n  \nelseif true (tokens(0) = \"End\") then\n  \nelseif true = ( \"Dim\" ) then\n\nend<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This sample replicates the same bug as the original BUT this time the compiler correctly complains about the comparison between TRUE and &#8220;DIM&#8221; (which is what my bug report was about originally)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select case can be a very handy way to declutter code and remove a lot of &#8220;if then&#8221; and &#8220;elseif then&#8221; from your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But only if you know how it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>And how they work &#8230;.. In a bug report I made I posted a tiny sample of code (Not this code has a known bug in it &#8211; Case &#8220;Dim&#8221; is wrong but it illustrated the bug I was reporting) I&#8217;ve used this style for very long sections of code esp where I dont want &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/07\/09\/select-case-vs-if-then\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Select case vs if then&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=794"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":795,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/794\/revisions\/795"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}