Marus Gradinaru
Marus Gradinaru

Reputation: 3022

How to pass variable length array constant as a parameter?

I have many constants of 2-dimensional array with one dimension variable in length, like this :

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));

And I want to pass this constants as a parameter to a procedure:

  LoadIntakes(Item, ThiamineRDA);

But how cand I define that procedure to permit that parameter ?

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

Upvotes: 3

Views: 304

Answers (2)

SilverWarior
SilverWarior

Reputation: 8331

if you wish to pass multidimensional open arrays as parameter to some procedure you first need to define special type for such array. You can then even define constants arrays of that type.

So your code would look like this:

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);

Upvotes: -2

Dsm
Dsm

Reputation: 6013

You can't pass open arrays that are open in 2 dimensions. But if one of the dimensions is fixed in size, you can do it, like this:

(I omitted your first parameter so that I could check that it does compile)

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;

Upvotes: 3

Related Questions