Delphi 能用类似下面示例的方式声明字符串数组吗?
{'alpha','beta','gamma'}
Delphi 能用类似下面示例的方式声明字符串数组吗?
{'alpha','beta','gamma'}
Is it possible for Delphi to declare a string array using something like the example below?
{'alpha','beta','gamma'}
试试这个
Const
EntryCount =4;
Labels : array [1..EntryCount] of string = ('label1','label2','label3','label4');
give this a go
Const
EntryCount =4;
Labels : array [1..EntryCount] of string = ('label1','label2','label3','label4');
也可以使用动态数组,如下例所示:
var
CachedLabels: TArray<string>;
function ReadLabels: TArray<string>;
begin
if Length(CachedLabels) = 0 then
CachedLabels := TArray<string>.Create('Alpha', 'Beta', 'Gamma');
Result := CachedLabels;
end;
虽然它是在运行时初始化堆上的动态数组,但这个示例展示了 Delphi 动态数组的“伪构造函数”,可以在使用的位置直接初始化。(注意,此示例并非线程安全)。
要取得数组长度,只需调用标准 Length() 函数,要确定有效的下标范围,则调用标准 Low() 和 High() 函数。
对于旧版 Delphi,请把 TArray 替换成自己定义的动态字符串数组类型,例如:
type
TLabelArray = array of string;
You can also use dynamic arrays, as in this example:
var
CachedLabels: TArray<string>;
function ReadLabels: TArray<string>;
begin
if Length(CachedLabels) = 0 then
CachedLabels := TArray<string>.Create('Alpha', 'Beta', 'Gamma');
Result := CachedLabels;
end;
Although it initializes a heap-allocated dynamic array at run-time, this demonstrates Delphi's dynamic-array "pseudo-constructor", which permits initialization right where it is used. (NOTE: this example is not thread-safe).
To obtain the array's length, simply call the standard Length() function, or call the standard Low() and High() functions to determine the valid range of indices.
For older Delphi versions, substitute your own dynamic string-array type for TArray, for example:
type
TLabelArray = array of string;
在 XE7 中,可以像下面这样定义常量动态数组:
const
Labels: TArray<String> = ['Alpha', 'Beta', 'Gamma'];
XE7 lets you define a constant dynamic array as follows:
const
Labels: TArray<String> = ['Alpha', 'Beta', 'Gamma'];
还有一种间接做法。编写这样的函数:
procedure copyLabels(var destination: ArrayOfString; const source: Array Of String);
var
count, index: Integer;
begin
count := Length(source);
SetLength(destination, count);
for index := count-1 downto 0 do
destination[index] := source[index];
end;
然后这样调用该函数:
copyLabels(labels, ['LabelA', 'LabelB', 'LabelC']);
其中:
labels: ArrayOfString;
There is also an indirect route. Write a function such as:
procedure copyLabels(var destination: ArrayOfString; const source: Array Of String);
var
count, index: Integer;
begin
count := Length(source);
SetLength(destination, count);
for index := count-1 downto 0 do
destination[index] := source[index];
end;
then invoke that function this way:
copyLabels(labels, ['LabelA', 'LabelB', 'LabelC']);
with:
labels: ArrayOfString;
这是我在 GDScript 中使用的枚举声明。 我想随机选择一个枚举变体,而不用列出所有可能的变体。这样以后增加变体时,就不必修改负责随机选择的代码。 我尝试过下面这种方法。 还有这一种。 两种都失败了。一个返回 Null,另一个报告 "Invali…
Nim 中怎样表示求余运算? cell % 7 == 0 会给出 undeclared identifier: '%' 这个错误 Google 搜索和查看 SO 都没找到答案。
怎样才能让函数在执行结束后返回一个值? 例如我有这样的代码。 ROUTINE N ? N ⍴ 12 //它会生成 N 个从 1 到 12 的随机值 (N 是函数的参数)。 我只是想知道怎样拿到函数的结果,然后比如把这个结果传给另一个函数。 谢谢大家的…