Delphi 可以实现这样的功能吗? (带有字符串和记录的动态数组)
type
TStringArray = array of String;
TRecArray = array of TMyRecord;
procedure DoSomethingWithStrings(Strings : TStringArray);
procedure DoSomethingWithRecords(Records : TRecArray);
function BuildRecord(const Value : String) : TMyRecord;
DoSomethingWithStrings(['hello', 'world']);
DoSomethingWithRecords([BuildRecord('hello'), BuildRecord('world')]);
我知道它不会那样编译。只是想问是否有一个技巧可以得到类似的东西。
如果您不必更改内部数组的长度DoSomethingWith*
例程,我建议使用开放数组而不是动态数组,例如像这样:
procedure DoSomethingWithStrings(const Strings: array of string);
var
i: Integer;
begin
for i := Low(Strings) to High(Strings) do
Writeln(Strings[i]);
end;
procedure DoSomethingWithRecords(const Records: array of TMyRecord);
var
i: Integer;
begin
for i := Low(Records) to High(Records) do
Writeln(Records[i].s);
end;
procedure Test;
begin
DoSomethingWithStrings(['hello', 'world']);
DoSomethingWithRecords([BuildRecord('hello'), BuildRecord('world')]);
end;
请注意array of string
在参数列表中 - 不TStringArray
!看文章“打开数组参数和const数组” http://rvelthuis.de/articles/articles-openarr.html,特别是有关“混乱”的部分,以获取更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)