【问题标题】:custom Iterator Index out of bounds自定义迭代器索引越界
【发布时间】:2014-03-04 15:32:46
【问题描述】:

我正在为我的班级作业制作一个自定义迭代器,但我的索引超出了界限,但我不知道在哪里。 当我调试它时,它在最后一次迭代时停止(计数 39,停止在索引 39)

我不确定问题在迭代器中的哪个位置仍然存在。

public override bool NextCourse(DataSet courseDS, DataView courseView, ref int currIndex)
{
    if (courseView.Count == 0)
        return false; 

    int nextIndex = currIndex + 1;


    if (nextIndex >= courseView.Count -1)
    {
        currIndex = nextIndex;
        return true;
    }

    if (currIndex < 0)
    {
        currIndex = 0;
        return true;
    }

    string currCourseNumber = (string)courseView[currIndex][(int)CourseListQueries.GetCourseListCols.CourseNumber];
    string courseNumber = String.Empty;

    currIndex++;

    do
    {
        courseNumber = (string)courseView[currIndex][(int)CourseListQueries.GetCourseListCols.CourseNumber];
        if ( String.Compare(courseNumber, currCourseNumber,true) != 0 ) 
            break;
        currIndex++;
    }
    while (currIndex < courseView.Count);

    return (currIndex < courseView.Count);
}// end NextCourse 

堆栈跟踪:

 Index 39 is either negative or above rows count.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index 39 is either negative or above rows count.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[IndexOutOfRangeException: Index 39 is either negative or above rows count.]
   System.Data.DataView.GetRow(Int32 index) +5259337
   System.Data.DataView.get_Item(Int32 recordIndex) +12
   FIMS_Courses.Controls.CourseTable.GradTable.GradTable.InitializeControls(GenericContainer container) +4978
   Telerik.Sitefinity.Web.UI.SimpleView.CreateChildControls() +52
   System.Web.UI.Control.EnsureChildControls() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +42
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

【问题讨论】:

  • 请包括堆栈跟踪,并指出您在哪里得到异常。 (如果可以的话,我会尝试使用迭代器块......这一切看起来都很复杂。)

标签: c# iterator


【解决方案1】:

您似乎确实对最终迭代进行了检查,但它输出的是 true。 你怎么称呼迭代器?我假设它在一个while循环结构中,因为它是一个布尔类型。

我认为如果您进行了此更改,它应该可以工作

if (nextIndex >= courseView.Count ) //to last element, yours is at second last.
    {
        currIndex = nextIndex;
        return false;// make it false, it should terminate and end loop . 
    }

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 2019-03-23
    相关资源
    最近更新 更多