gpt4 book ai didi

arrays - Delphi 中字符串和记录的常量就地数组

转载 作者:行者123 更新时间:2023-12-03 15:26:07 25 4
gpt4 key购买 nike

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;

请注意参数列表中的字符串数组 - 而不是TStringArray!参见文章"Open array parameters and array of const" ,特别是有关“困惑”的部分,以获取更多信息。

关于arrays - Delphi 中字符串和记录的常量就地数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com