関数のパラメータを省略可能にしたい
|
R> 関数のパラメータを省略可能なものにしたいのですが、どうしたら良いのでしょうか。
R> また、パラメータの個数を動的に決められるものにするには、どうしたら良いのでしょうか。
Delphiで可変数パラメータを使うには、array of constを使うのが一般的だと思います。
function Test(Values: array of const): string;
var i:Integer;
begin
Result:='';
for i:=Low(Values) to High(Values) do
with Value do
case VType of
vtInteger:
Result:=Result+IntToStr(VInteger);
vtChar:
Result:=Result+VChar;
vtString:
Result:=Result+VString^;
vtObject:
if VObject is TComponent then
Result:=Result+(VObject as TComponent).Name;
...
end;
end;
Test(['Testing ',123,' ',Self);
あってるかな?
ちなみに、C++Builder3/Delphi4 以降では、
procedure AProcedure(a: Integer; b: Integer= 3);
のような形で省略可能なパラメータを定義できるそうです。 |
|