jpf*_*ius 4 arrays delphi delphi-xe
德尔福可以这样吗?(使用动态字符串和记录数组)
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')]);
Run Code Online (Sandbox Code Playgroud)
我知道它不会像那样编译.只想问是否有一个技巧可以得到类似的东西.
如果您不必更改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;
Run Code Online (Sandbox Code Playgroud)
请注意array of string参数列表中的 - 不是TStringArray!有关详细信息,请参阅文章"打开数组参数和const数组",尤其是有关"混淆"的部分.
| 归档时间: |
|
| 查看次数: |
2412 次 |
| 最近记录: |