0
我有一个for循环,我用它来获取列表项以显示在我的线形图中。这很简单,但我得到一个索引超出范围的错误。我理解在for循环中发生的事情的方式是,只要我少于indexCount()就可以继续计数。我为什么要通过我的索引方法?我研究并使用了断点没有找到,我=我的集合大小。For循环没有正确地返回索引
List<ChartData> points = ChartData.getData();
for (int i = 0; i < chartData.indexCount(); i++)
{
series0.AddItem(points[i].Produced);
series1.AddItem(points[i].Labeled);
RadChart1.PlotArea.XAxis.Items.Add(new ChartAxisItem(points[i].CasesLabeled.ToString()));
}
我做了一个自定义索引,这样我就可以从数据库中获取大部分行来显示。因此在那里有chartData.indexCount()
方法。这里是我的indexCount方法,这里可能出错了?
public int indexCount()
{
StringBuilder sqlString = new StringBuilder();
sqlString.Append("SELECT Count(Number) FROM SomeDB.dbo.Order");
SqlDataReader reader = null;
SqlConnection dbConn = DBHelper.getConnection();
try
{
reader = DBHelper.executeQuery(dbConn, sqlString.ToString(), null);
if (reader != null)
{
while (reader.Read())
{
number = reader.GetInt32(0);
}
}
reader.Close();
reader.Dispose();
dbConn.Close();
dbConn.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return number;
}
你看它与断点? –
'points.Count'和'chartData.indexCount()'可能会有所不同。如果第二个更大,那么显然你会得到'IndexOutOfRangeException'。 –
循环到'points.Count'而不是'chartData.indexCount()'会更安全。 – TypeIA