Crystal Reports错误:下标必须介于1和数组大小之间

时间:2013-01-25 08:21:46

标签: arrays function crystal-reports

以下函数在Crystal Reports XI中运行时返回“下标必须介于1和数组大小之间”-error。知道为什么以及如何解决它?

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;

注意: 我发现如果在数组中的第二个日期之前运行,上面的函数会返回比开始日期更早的停止日期。我重写了函数来反击,然后我没有产生错误的情况,但我仍然感兴趣的是为什么在这个函数中发生错误以及如何处理它。

1 个答案:

答案 0 :(得分:2)

NumberVar startIndex;
if stopIndex = 1 then
    startIndex = 1
else
    startIndex = stopIndex - 1;

当然应该是

NumberVar startIndex;
if stopIndex = 1 then
    startIndex := 1
else
    startIndex := stopIndex - 1;

现在它有效......

NumberVar stopIndex;

也应改为

NumberVar stopIndex := 1;

以避免在首次报告日期之前运行报告时出错。