All very clever, but way, way too complicated and – if you don’t mind me saying so – as ugly as sin into the bargain (imho – ymmv).
For simple cases [sic], it is actually relatively straightforward to uses strings in a case statement.
Just add this function to a convenient unit:
interface function StringIndex(const aString: String; const aCases: array of String; const aCaseSensitive: Boolean = TRUE): Integer; implementation function StringIndex(const aString: String; const aCases: array of String; const aCaseSensitive: Boolean): Integer; begin if aCaseSensitive then begin for result := 0 to Pred(Length(aCases)) do if ANSISameText(aString, aCases[result]) then EXIT; end else begin for result := 0 to Pred(Length(aCases)) do if ANSISameStr(aString, aCases[result]) then EXIT; end; result := -1; end;
And now you can use this in an ordinary case statement:
case StringIndex(SomeString, [SelectorA, SelectorB, SelectorC]) of 0: // for SelectorA 1: // for SelectorB 2: // for SelectorC else // Some other SomeString end;
I added a case-sensitivity switch, but more sophisticated options could be provided such as partial matching. Similarly, there could possibly be some optimisation of the string matching within the function (perhaps triggered by a number of cases above a certain threshold), but this is the bare bones of what you need.
In fact, I am pretty sure I’ve seen something similar to this in the past, but I can’t put my finger on when or where.
In any event, build on, improve, embellish to your hearts content, and all with perfectly clear, concise, Pascal-like syntax in all versions of Delphi.