2014-03-04 39 views
0

我正在爲我的類分配做一個自定義迭代器,並且我得到一個索引超出界限,但我看不到在哪裏。 當我調試它停止在最後一次迭代(計數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 
+0

請包括堆棧跟蹤,並指明你要除外。 (如果可能的話,我會嘗試使用迭代器塊...這一切看起來都非常複雜。) –

回答

1

你似乎有對最後一次迭代的檢查,但它是真實的輸出。 你怎麼調用迭代器?我假設它是在一個while循環結構中,因爲它是一個bool類型。

我認爲,如果你做這個改變它應該工作

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 . 
    } 
+0

是的,這有效。謝謝 – Protonblast