错误位置1没有行

时间:2012-11-19 09:58:23

标签: c# .net visual-studio-2010

我收到了异常

System.ArgumentOutOfRangeException:位置1没有行。    RBTree`1.GetNodeByIndex(Int32 userIndex)**

位置上没有行可以是0或1或2。我想我正在尝试读取或写入一些数组边界之外的数组元素。代码段如下所示

public void ManageAlarm(string textName, int leaveValue)
{
   try
   {
      int indices = team.Find(textName);
      if (indices >= 0)
      {
         DataRow row = teamTable.Rows[indices];
         row[m_leaveValues] = leaveValue;
      }
   }

我应该怎么做以防止此警报跟踪

1 个答案:

答案 0 :(得分:3)

在访问其中的行之前,您需要检查m_tblAlert中的行数。 m_tblAlert.Rows.Count must be greater then indx

public void ManageDuplicateAlarm(string alertName, int dupValue)
{
   try
   {
      int indx = m_alerts.Find(alertName);
      if (indx >= 0 && m_tblAlert.Rows.Count > indx)
      {
         DataRow row = m_tblAlert.Rows[idx];
         m_dcDuplicates.ReadOnly = false;
         row[m_dcDuplicates] = dupValue;
         m_dcDuplicates.ReadOnly = true;
      }
   }

在OP评论中编辑更多的exlanation

对于语句m_tblAlert.Rows [idx],您是checking indx >= 0 to make sure that that -1 could not be row index;您拥有数据表中的Similarly you need to check if the value return by m_alerts.Find(alertName) must be valid row number i.e it should not be greater then the number of rows