如何将变长数组常量作为参数传递?

时间:2018-06-13 12:22:11

标签: arrays delphi delphi-2009

我有许多二维数组的常量,其长度为一维变量,如下所示:

const
  ThiamineRDA: array[0..2, 0..3] of Double =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3), (0, 1, 3, 0.5));
  RiboflavinRDA: array[0..1, 0..3] of Double =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3));

我想将此常量作为参数传递给过程:

  LoadIntakes(Item, ThiamineRDA);

但是我如何定义允许该参数的程序呢?

procedure LoadIntakes(Item:PNutrientInfo; IntakesList: array of... ???? );
begin
 //.....
end;

2 个答案:

答案 0 :(得分:2)

您无法传递以二维方式打开的开放数组。但是,如果尺寸中的一个尺寸固定,您可以这样做:

(我省略了你的第一个参数,以便我可以检查它是否编译)

type
  TQArray = array[0..3] of double;
const
  ThiamineRDA: array[0..2] of TQArray =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3), (0, 1, 3, 0.5));
  RiboflavinRDA: array[0..1] of TQArray =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3));

procedure LoadIntakes( IntakesList: array of TQArray );
begin
 //.....
end;

procedure Test;
begin
  LoadIntakes( ThiamineRDA );
end;

答案 1 :(得分:-2)

如果您希望将多维打开数组作为参数传递给某个过程,首先需要为此类数组定义特殊类型。然后,您甚至可以定义该类型的常量数组。

所以你的代码看起来像这样:

type
  ThiamineRDA = array[0..2, 0..3] of Double;
  RiboflavinRDA = array[0..1, 0..3] of Double;

...

const
  ArThiamineRDA: ThiamineRDA =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3), (0, 1, 3, 0.5));
  ArRiboflavinRDA: RiboflavinRDA =
   ((0, 0, 0.05, 0.2), (0, 0.06, 0.11, 0.3));

...

  LoadIntakes(Item, ArThiamineRDA);