【发布时间】:2015-07-06 21:49:27
【问题描述】:
我将 QuaterDisplay 输入为虚拟数据,但出现如下错误:
索引超出了数组的范围。
代码:
private string GetQuarterDisplay(DateTime dateKey)
{
return ((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select("DateKey = '"
+ dateKey + "'"))[0].QuarterDisplay; //error occur here
}
internal void PropagateModelStartQuarter()
{
object[] args = new object[0];
m_privateObject.Invoke("PropagateModelStartQuarter", new System.Type[0], args);
}
【问题讨论】:
-
那么你的问题是什么,请清理你的代码,看起来它已经经历了战争
-
它给了你这个错误,因为你试图访问你的数组中不存在的索引。如果您有 5 个索引的长度,
[0], [1], [2], [3], [4]并尝试访问[5],那么这会给您错误Index was outside the bounds of the array.。确保您的AvailabilityDS.IntelTimeRow[]数组中确实有数据 -
什么是
mAvailabilityDS.Time.Select("DateKey = '"dateKey"'"))?里面有数据吗? -
您可能想要使用 LINQ:
return mAvailabilityDS.Time.FirstOrDefault(x => x.DateKey == dateKey) -
Time 表中有多少行带有 DateKey,例如 12/31/2006 12:00:00 AM 4/1/2007 12:00:00 AM 7/1/2007 12:00 2007 年 9 月 30 日上午 12:00:00 2007 年 12 月 30 日上午 12:00:00 2008 年 3 月 30 日上午 12:00:00 2008 年 6 月 29 日上午 12:00:00 9/28 /2008 12:00:00 AM 12/28/2008 12:00:00 AM 3/29/2009 12:00:00 AM 3/29/2009 12:00:00 AM
标签: c# unit-testing