恒就地阵列在Delphi中的字符串和记录 [英] Constant in-place array of strings and records in Delphi
问题描述
是这样的事情可能与德尔福? (用绳子和记录的动态数组)
Is something like this possible with Delphi? (with dynamic arrays of strings and records)
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')]);
我知道,它不会编译这样。只是想问问,如果有一招得到类似的东西。
I know that it does not compile like that. Just wanted to ask if there's a trick to get something similar to that.
推荐答案
如果你没有改变你的 DoSomethingWith *
程序里面的数组长度,我建议使用开放数组,而不是动态的,例如:像这样的:
If you don't have to change the length of the arrays inside your DoSomethingWith*
routines, I suggest using open arrays instead of dynamic ones, e.g. like this:
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
!请参阅文章开放数组参数和常量数组,尤其是关于混乱一节,以获取更多信息。
Please note the array of string
in the parameter list - not TStringArray
! See the article "Open array parameters and array of const", especially the section about "Confusion", for more information.
这篇关于恒就地阵列在Delphi中的字符串和记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!