Author: Michael Britt
You've probably tried providing a Case statement with string type selector
expression, to find out that it only takes ordinal types (which string is not).
The following function enables you to use the Case statement with string type
variables:
Answer:
1 function StringToCaseSelect
2 (Selector: string;
3 CaseList: arrayofstring): Integer;
4 var5 cnt: integer;
6 begin7 Result := -1;
8 for cnt := 0 to Length(CaseList) - 1 do9 begin10 if CompareText(Selector, CaseList[cnt]) = 0 then11 begin12 Result := cnt;
13 Break;
14 end;
15 end;
16 end;
17 18 //Usage:19 20 case StringToCaseSelect('Delphi',
21 ['About', 'Borland', 'Delphi']) of22 0: ShowMessage('You''ve picked About');
23 1: ShowMessage('You''ve picked Borland');
24 2: ShowMessage('You''ve picked Delphi');
25 end;