本文介绍了必须是非负数且小于集合的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Whats wrong with this code why this exception occur .
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List`1.get_Item(Int32 index) at CourseAcademics_ProgressReports.EvalResult(List`1[] a, List`1[] totalmarks, List`1 names, Int32 subjectcount, DataTable dt) in c:\Users\Administrator\Documents\Visual Studio 2010\Projects\RootsERPWeb\ERPWeb\CourseAcademics\ProgressReports.aspx.cs:line 315
public void EvalResult(List<double>[] a, List<double>[] totalmarks, List<string> names, int subjectcount, DataTable dt)
{
dt1 = new DataTable();
try
{
for (int i = 0; i < a[0].Count; i++)
{
string[] xxx = new string[subjectcount + 4];
xxx[0] = names[i];
for (int j = 1; j <= subjectcount; j++)
{
double perc = Math.Round((a[j - 1][i] / totalmarks[j - 1][i]) * 100, 2);
xxx[j] = a[j - 1][i] + "/" + totalmarks[j - 1][i];
}
double obtainedmarkstotal = 0;
double totalmark = 0;
for (int k = 0; k < subjectcount; k++)
{
obtainedmarkstotal += a[k][i];
totalmark += totalmarks[k][i];
}
double perctotal = Math.Round(obtainedmarkstotal / totalmark * 100, 2);
xxx[subjectcount + 1] = obtainedmarkstotal + "/" + totalmark;
xxx[subjectcount + 2] = perctotal.ToString() + "%";
xxx[subjectcount + 3] = getgrade(perctotal);
dt.Rows.Add(xxx);
}
gvforawards.Visible = true;
Label1.Text = "";
dt1 =(DataTable) dt;
Session["result"]=dt1;
gvforawards.DataSource = dt1;
gvforawards.DataBind();
int s = 0;
int columns_count = gvforawards.Columns.Count;
foreach (GridViewRow gvr in gvforawards.Rows)
{
((HyperLink)gvr.Cells[0].Controls[0]).NavigateUrl="~/MarkSheet.aspx?;
s++;
}
}
catch(Exception ex)
{
gvforawards.Visible = false;
Label1.ForeColor = Color.Red;
Label1.Visible = true;
Label1.Text = ex.ToString();
}
}
我尝试了什么:
当j值为9然后发生异常。任何人都可以告诉我我做了什么错误?
推荐答案
这篇关于必须是非负数且小于集合的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!