我在我的程序中拋出索引超出綁定異常在 行yearList.SetValue(年[count],計數)下面的代碼;索引越界異常在C#
protected void invoiceYear_DataBound(object sender, EventArgs e)
{
//invoiceYear.SelectedItem.Value= GetYearRange();
String[] years = GetYearRange().Split(new char[] { '[', ',', ']',' ' });
ListItem [] yearList = new ListItem[]{};
System.Diagnostics.Debug.WriteLine("years-->" + years.Length);
for (int i = 0; i < years.Length; i++)
{
System.Diagnostics.Debug.WriteLine("years-->" + years.GetValue(i));
}
int count = 0;
foreach (String str in years)
{
if (string.IsNullOrEmpty(str))
System.Diagnostics.Debug.WriteLine("empty");
else
{
yearList.SetValue(years[count], count);
count++;
}
}
//System.Diagnostics.Debug.WriteLine("yearList-->" + yearList.GetValue(0));
//invoiceYear.Items.AddRange(yearList);
}
您還沒有真正問過問題。您可能希望更新信息以包含問題。 –
您是否嘗試過調試以查看** count **的值在使用它訪問* years *數組時的值? –
嘗試做計數-1 –