Using strings as discriminant variables in Delphi’s Case statement Chen Jiaxin╱[email protected]● When mentioning Case statements in the introduction, the first application case that comes to mind is through a Determine variables to allow the program flow to choose a suitable path to continue execution according to different conditions. Or, when there is a series of if and else if statement combinations in our program, we often think of using Case statements to simplify the program code and improve its readability and execution efficiency. However, in some cases, Case statements cannot be applied to our programs because the discriminant variables they allow are limited to any form of xPRession) and ordinal type. This is the so-called "Ordinal types" refer to "ordered" ones such as integers, characters, enumerations, Boolean and sets, and can be applied to things like Ord(), Pred(), Succ(), Low() and High() The type of expression (see [1]). Unfortunately, string (string) is obviously not an ordinal type, and at some point (an example will be given below), when the type of the condition variable is a string and there are quite a few conditional branches, although it is helpless, however There seems to be no other way besides using a lot of if and else if statements, alas. For example, the following code is not allowed in Delphi: #001 var #002 Str: String; // Declare a discriminant variable of String type #003 begin #004 case Str of // Error message: Ordinal type required #005 / / ... #006 end; #007 end;This simply cannot be compiled, so the traditional solution is usually to convert it into a combination of a large number of if and else if statements. At this point I really wish we were using Visual Basic, because the following code is allowed by its compiler (see [5]): #001 Dim Str As String ' Declares a discriminant variable of type String #002 Select Case Str ' is equivalent to Delphi's Case statement #003 '... #004 End Select ' OK, through compilation. So in this article, the author tries to use this problem as a starting point, in "In addition to converting into a combination of if and else if statements "Looking for other feasible solutions besides traditional solutions", and sincerely hope that this article can be helpful to people who are facing this problem and are eager to find a solution (after reading [12]-[14], the author further found that this is indeed the case) Helps. Let me give you a preview first. I plan to introduce 7 solutions. The first 5 methods are excerpts or extensions from [9]-[13], and the sixth method is a comprehensive multi-loader implemented by the author. (overloading) version. These options are: v Option 1: Search string array v Option 2: Use real index v Option 3: Use hash function v Option 4: Nested case statement v Option 5: Use TStringListv Option 6: Implement multi-load function Solution 7: Apply ready-made instructions. This article will start by reviewing topics such as conditional statements (main content refer to [1] and [4]) and recognition strings (main content refer to [2]). Those who are familiar with Delphi can skip it. In this section, read the section "Performance Issues" directly to the end of the article.