# Array Problem

I have the following formula, which calculates the number of days by taking the max and minimum dates from 5 dates and subtracts them from each other to work out number of days.

My problem is that I keep getting an error message saying size of array must be greater 0 and
Maximum(odates) is highlighted.

Can anybody help with this

Thanks

local numbervar i;
local numbervar j;
local datevar array idates := [{table.date1},{table.date2},{table.date3},{table.date4},{table.date5}];
local datevar array odates ;

for i := 1 to 5
do
(
if idates[i] > date(0,0,0) then
(
j := j + 1;
redim preserve odates[j];
odates[j] := idates[i]
)
);

Maximum(odates) - Minimum(odates)
###### Who is Participating?

Commented:
You do need the semicolon after

odates[j] := idates[i];

Also need it after the second to last ).  Correct syntax should be:

local numbervar i;
local numbervar j;
local datevar array idates := [{table.date1},{table.date2},{table.date3},{table.date4},{table.date5}];
local datevar array odates ;

for i := 1 to 5
do
(
if idates[i] > date(0,0,0) then
(
j := j + 1;
redim preserve odates[j];
odates[j] := idates[i];

if odates[j] > MaxDateSoFar then
MaxDateSoFar := odates[j];
if odates[j] < MinDateSoFar then
MinDateSoFar := odates[j];
);
);

MaxDateSoFar - MinDateSoFar
0

Commented:
As far as I know you can't apply the 'Maximum' function to an array - this function within Crystal takes the maximum from all of the values a recordset contains for a specific field.  I believe you're going to need to loop through your array and set the values there:

for i := 1 to 5
do
(
if idates[i] > date(0,0,0) then
(
j := j + 1;
redim preserve odates[j];
odates[j] := idates[i]
// ---------------- ADD LINES BELOW ----------
if odates[j] > MaxDateSoFar then
MaxDateSoFar := odates[j];
if odates[j] < MinDateSoFar then
MinDateSoFar := odates[j];
)
);

The Max and Min are local variables that you'll want to set to some extreme values initially.  Finish your formula by subtracting the Min from the Max.
0

Commented:
Agree

mlmcc
0

Author Commented:
Thanks for the answers, I have changed the code as below and get the message the ) is missing, if is highlighted

local numbervar i;
local numbervar j;
local datevar array idates := [{table.date1},{table.date2},{table.date3},{table.date4},{table.date5}];
local datevar array odates ;

for i := 1 to 5
do
(
if idates[i] > date(0,0,0) then
(
j := j + 1;
redim preserve odates[j];
odates[j] := idates[i]

if odates[j] > MaxDateSoFar then
MaxDateSoFar := odates[j];
if odates[j] < MinDateSoFar then
MinDateSoFar := odates[j];
)
);

MaxDateSoFar - MinDateSoFar
0

Commented:
If the code is pasted from the formula then you are missing a ; on

odates[j] := idates[i]

You might also want to delete the ; before the first )

If that doesn't work then maybe you need a ; after the first )

mlmcc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.