This will only change the TField array item, but not the local variable FieldX as expected.
A const or var array of TField is no replacement for a couple of var par: TField parameters. As soon as you construct that array parameter, the addresses of the local variables are gone, while their current content (which may be undefined here) is copied to the array item.
Perhaps this may be a better approach (didn't try as I am too lazy to create a fake dataset with these fields):
procedure ConnectFields(Query: TDataSet; const Fields: TArray<^TField>; const Names: TArray<string>);
begin
Assert(Length(Fields) = Length(Names), 'Number of fields and number of names must match');
for var ix := 0 to Length(Fields) - 1
do begin
Fields[ix]^ := Query.FindField(Names[ix]);
if not Assigned(Fields[ix]^) then
raise Exception.Create(Format('Field %s not found.', [Names[ix]]));
end;
end;
procedure Test;
var
Field1, Field2, Field3, Field4: TField;
begin
ConnectFields(Query,
[ @Field1, @Field2, @Field3, @Field4],
['Field1', 'Field2', 'Field3', 'Field4']);
...
end;