索引超出了数组的边界如何解析

本文关键字:边界 何解析 数组 索引 | 更新日期: 2023-09-27 18:04:38

我有一个代码。当我执行代码时,我得到一个错误。下面是我的代码

if (!Page.IsPostBack)
{
  Session.RemoveAll();
  DateTime today = DateTime.Today;
  string[] strArray = new string[4];
  int num1 = checked (Convert.ToInt32(Strings.Split(Conversions.ToString(today), "/", -1, CompareMethod.Binary)[4]) - 4);
  DropDownList ddlYear = _ddlYear;
  ddlYear.Items.Clear();
  int num2 = (int) checked ((short) num1);
  short num3 = checked ((short) (num1 + 6));
  for (short index = (short) num2; (int) index <= (int) num3; ++index)
    ddlYear.Items.Add(Conversions.ToString((int) index));
  ddlYear.DataBind();
  ddlYear.SelectedValue = Conversions.ToString((int) Convert.ToInt16(DateTime.Now.Year));
  LoadDept();
  Session.Add("rno", (object) 0);
  Session["rno"] = (object) new Random().Next();
  Rno = Conversions.ToInteger(Session["rno"]);
}

索引超出了数组的边界如何解析

检查你的行分割值:

int num1 = checked (Convert.ToInt32(Strings.Split(Conversions.ToString(today), "/", -1, CompareMethod.Binary)[4]) - 4);

从异常消息中可以很明显看出,拆分返回的条目不超过5个(没有4个索引)

将索引从4

Strings.Split(Conversions.ToString(today), "/", -1, CompareMethod.Binary)[4]

低价值(例子: 3

Strings.Split(Conversions.ToString(today), "/", -1, CompareMethod.Binary)[3]

您的string.Split()方法可能返回少于5项(可能是3项,因为DateTime/上的分割)。因为C#0开始计数,所以第四个元素的索引为3

Edit:检查第二个原因,这可能是你的例子中的原因

第一次检查:
你怎么确定弦乐乐队。

为了避免这种IndexOutOfRangeException,你应该以一种方式格式化你的日期,你的日期总是以5(或更多)长度的数组格式化,并以"/"分隔。

但是,即使日期格式很好,您也应该始终将数组恢复为var,检查大小是否符合您的要求,然后只恢复硬编码值:

if (!Page.IsPostBack)
{
  Session.RemoveAll();
  DateTime today = DateTime.Today;
  string[] strArray = Strings.Split(Conversions.ToString(today), "/", -1, CompareMethod.Binary);
  if(strArray.Length >= 5)
  {
      int num1 = checked (Convert.ToInt32(strArray[4]) - 4);
      DropDownList ddlYear = _ddlYear;
      ddlYear.Items.Clear();
      int num2 = (int) checked ((short) num1);
      short num3 = checked ((short) (num1 + 6));
      for (short index = (short) num2; (int) index <= (int) num3; ++index)
        ddlYear.Items.Add(Conversions.ToString((int) index));
      ddlYear.DataBind();
      ddlYear.SelectedValue = Conversions.ToString((int) Convert.ToInt16(DateTime.Now.Year));
      LoadDept();
      Session.Add("rno", (object) 0);
      Session["rno"] = (object) new Random().Next();
      Rno = Conversions.ToInteger(Session["rno"]);
  }
}

第二次检查:
您可能会出现此错误的另一个原因可能是由于您不理解数组第一次出现的数字是什么。
答案是0。所以如果你想要得到第四个值,你需要输入3:

if (!Page.IsPostBack)
{
  Session.RemoveAll();
  DateTime today = DateTime.Today;
  string[] strArray = Strings.Split(Conversions.ToString(today), "/", -1, CompareMethod.Binary);
  if(strArray.Length >= 4)
  {
      int num1 = checked (Convert.ToInt32(strArray[3]) - 4);
      DropDownList ddlYear = _ddlYear;
      ddlYear.Items.Clear();
      int num2 = (int) checked ((short) num1);
      short num3 = checked ((short) (num1 + 6));
      for (short index = (short) num2; (int) index <= (int) num3; ++index)
        ddlYear.Items.Add(Conversions.ToString((int) index));
      ddlYear.DataBind();
      ddlYear.SelectedValue = Conversions.ToString((int) Convert.ToInt16(DateTime.Now.Year));
      LoadDept();
      Session.Add("rno", (object) 0);
      Session["rno"] = (object) new Random().Next();
      Rno = Conversions.ToInteger(Session["rno"]);
  }
}

第三次检查:
为什么要特别使用字符串?Split(这是一个VisualBasic方法)。最好使用string。split。
顺便说一下,DateTime对象已经有了格式化方法,比如DateTime。ToLongDateString:

if (!Page.IsPostBack)
{
  Session.RemoveAll();
  DateTime today = DateTime.Today;
  string[] strArray = today.ToLongDateString().Split("/".ToCharArray());
  if(strArray.Length >= 4)
  {
      int num1 = checked (Convert.ToInt32(strArray[3]) - 4);
      DropDownList ddlYear = _ddlYear;
      ddlYear.Items.Clear();
      int num2 = (int) checked ((short) num1);
      short num3 = checked ((short) (num1 + 6));
      for (short index = (short) num2; (int) index <= (int) num3; ++index)
        ddlYear.Items.Add(Conversions.ToString((int) index));
      ddlYear.DataBind();
      ddlYear.SelectedValue = Conversions.ToString((int) Convert.ToInt16(DateTime.Now.Year));
      LoadDept();
      Session.Add("rno", (object) 0);
      Session["rno"] = (object) new Random().Next();
      Rno = Conversions.ToInteger(Session["rno"]);
  }
}

我想你可以用

代替你的代码
 int curYear = DateTime.Today.Year;
 int endYear = curYear + 6;
 for (int x = curYear; x <= endYear; x++)
    ddlYear.Items.Add(x.ToString());

通过转换日期创建的字符串不需要拆分。
只需使用Year属性和一个简单的循环。

你也可以用:

删除循环
 var range = Enumerable.Range(DateTime.Today.Year, 6)
 ddlYear.Items.AddRange(range.Select(x => new ListItem(x.ToString())).ToArray());