加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 09:24:24 所属栏目:大数据 来源:网络整理
导读:德尔福可以这样吗? (使用动态字符串和记录数组) type TStringArray = array of String; TRecArray = array of TMyRecord;procedure DoSomethingWithStrings(Strings : TStringArray);procedure DoSomethingWithRecords(Records : TRecArray);function Build
德尔福可以这样吗? (使用动态字符串和记录数组)

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”,尤其是有关“混淆”的部分.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读