类型变化的开放数组参数

View Code
procedure ShowMsg(const fmtStr:string; const params: array of const);
begin
ShowMessageFmt(fmtStr,params);
end;

function SumAll(const Args: array of const): Extended;
var
I: Integer;
begin
Result := 0;
for I := Low(Args) to High(Args) do
case Args[I].VType of
vtInteger: Result :=
Result + Args[I].VInteger;
vtBoolean:
if Args[I].VBoolean then
Result := Result + 1;
vtChar:
Result := Result + Ord(Args[I].VChar);
vtExtended:
Result := Result + Args[I].VExtended^;
vtString, vtAnsiString:
Result := Result + StrToIntDef((Args[I].VString^), 0);
vtWideChar:
Result := Result + Ord(Args[I].VWideChar);
vtCurrency:
Result := Result + Args[I].VCurrency^;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
d: Double;
e: Extended;
begin
i := 99;
d := 10.8;
e := SumAll([1,2,3,4,5]);
ShowMessageFmt('%f',[e]);

e := SumAll([i,2.5,3,d,5]);
ShowMessageFmt('%f',[e]);

ShowMsg('Integer:%d, Double:%f',[i,d]);
end;
原文地址:https://www.cnblogs.com/Jekhn/p/2324890.html