There are some proposals how to use case with strings in Delphi. Have a look at my suggestion:
I use a function StrCase with one parameter of type open string array. The function checks the string parameter (the selector) against the values in the array. The result is the position of the string whether found within the array (0 to count - 1). If the array doesn't contain the selector the result is -1.
function StrCase(Selector: string; StrList: array of string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to High(StrList) do begin
if Selector = StrList[I] then begin
Result := I;
break;
end;
end;
end;
Now it is possible to use the function StrCase instead of the case selector:
procedure TestString(StringToTest: string);
begin
case StrCase(StringToTest, ['One', 'Two', 'Three']) of
0: ShowMessage('1: ' + s);
1: ShowMessage('2: ' + s);
2: ShowMessage('3: ' + s);
else
ShowMessage('else: ' + s);
end;
end;
That's all. Use the function everywhere a "String-Case" is necessary.
