Einzelnen Beitrag anzeigen

Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.021 Beiträge
 
Delphi 12 Athens
 
#6

AW: array of const für spätere Verwendung speichern

  Alt 1. Feb 2016, 14:34
TValue: Dieses Array kann man aber nicht an ein array of const übergeben?
Nicht direkt, aber über eine Schleife lässt sich natürlich einfach wieder ein array of TVarRec aufbauen:

Delphi-Quellcode:
type
  TRec = record
  private
    function GetAsVarRecs: TArray<TVarRec>;
  public
    Params: array of TValue;
    constructor Create(const AParams: array of const);
    property AsVarRecs: TArray<TVarRec> read GetAsVarRecs;
  end;

constructor TRec.Create(const AParams: array of const);
var
  I: Integer;
begin
  SetLength(Params, Length(AParams));
  for I := Low(AParams) to High(AParams) do begin
    Params[I] := AParams[I];
  end;
end;

function TRec.GetAsVarRecs: TArray<TVarRec>;
var
  I: Integer;
begin
  SetLength(result, Length(Params));
  for I := Low(Params) to High(Params) do begin
    result[I] := Params[I].AsVarRec;
  end;
end;

procedure Test(const Fmt: string; const Args: array of const);
var
  rec: TRec;
  S: string;
begin
  rec := TRec.Create(Args);
  S := Format(Fmt, rec.AsVarRecs);
  Assert(S = Format(Fmt, Args));
end;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat