以下函数在 Crystal Reports XI 中运行时返回“下标必须介于 1 和数组大小之间”-错误。知道为什么以及如何解决它吗?
Function (optional BooleanVar start := true)
DateVar Array reportdates := [
cDate(2012, 10, 22),
cDate(2012, 11, 15),
cDate(2013, 01, 23),
cDate(2013, 02, 20),
// some more lines of dates...
cDate(2014, 01, 02)
];
// Here is some code that sorts the array just to be sure.
// Removed from question
// Find index of last reportdate not later than today
NumberVar stopIndex;
for i := 1 to UBound(reportdates) do (
if CurrentDate >= reportdates[i] then
stopIndex := i
);
DateTimeVar returnDateTime;
if start = true then ( // return start date
NumberVar startIndex;
if stopIndex = 1 then
startIndex = 1
else
startIndex = stopIndex - 1;
//*** The error occurs here
returnDateTime := cDateTime(reportdates[startIndex], cTime(0,0,0));
//*** The error occurs here
)
else ( // return stop date
DateVar stopDate = reportdates[stopIndex];
returnDateTime := dateAdd("d", -1, cDateTime(reportdates[stopIndex], cTime(23,59,59)));
);
returnDateTime;
注意:我发现如果在数组中的第二个日期之前运行,上述函数会返回比开始日期更早的停止日期。我重写了函数来解决这个问题,然后我没有遇到产生问题的错误的情况,但我仍然会对为什么在这个函数中发生错误以及如何处理它感兴趣。