Is there a reason this can’t work?
The reason is because a case statement is designed to compare against a constant and, in the code you posted, the array - being a typed constant - is actually a variable and the "name" field is also a variable, therefore a "case" statement will be "unhappy" about that situation.
you can easily emulate a variable against variable case statement by creating an inline scope. e.g,
for i := 1 to 1 do // the fake scope, a loop that executes only once (NOTE: can use a repeat ... until TRUE instead)
begin
if <somevar> = <someothervar> then
begin
DoSomething();
break; // the break is important
end;
if <somevar2> = <whoknowswhat> then
begin
DoTheOtherThing();
break();
end;
{ more cases here }
// the "otherwise" case
DoTheOtherwiseCase();
// a break is not needed for the emulated "otherwise" but, I recommend you code it just in case it becomes a case instead of the general "otherwise"
break;
end;
// code outside the "case" here, which is where the above "break"(s) land
The above is just like a case without any of the limitations of a case. That said, the compiler will not optimize such a "case" statement into a jump table but, it can't do that anyway whenever variables are involved, therefore there is no loss.
HTH.
Just for fun: Delphi 2 (and likely newer versions) optimizes one of these fake scopes by removing the loop then the compiler issues an internal error because it cannot find the target of the "break" (because it removed the loop) <chuckle>
Lastly instead of using "for i := 1 to 1 do", it is a bit clearer to write "for SCOPE := SCOPE to SCOPE do" that should really clue the reader that the "for" isn't a loop but a scope (or repeat... until SCOPE = SCOPE;)