繁体   English   中英

错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小

[英]Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection

我在以下代码中遇到一个异常:“索引超出范围。必须为非负数,并且小于集合的大小。”代码中实际上是怎么回事,处理了一些重复的值,这些值最终被占用数据网格

try
         {
            int index = alerts.Find(alertName);
            if (index >= 0 && tblAlarm.Rows.Count > idx)
            {
               DataRow row = tblAlarm.Rows[idx];
               m_dcDuplicates.ReadOnly = false;

            }
         }

我需要将int等类型的大小增加到long吗? 还是需要任何其他签入代码?

由于您使用的是lock语句,因此大概是多线程实现。

可能的原因是您无法正确同步对对象的访问。 再看那将更新集合任何其他代码( this在上面的代码) -如果这个问题并不明显张贴。

更新

例如,在更新的源代码中,索引器的设置器未同步:

public Alert this[int index]
{
    get ...
    set
    {
        this.List[index] = value;
    }
}

您可能需要以下内容:

public Alert this[int index]
{
    get ...
    set
    {
        lock(this)
        {    
            this.List[index] = value;
        }
    }
}

代码中的另一个奇怪之处是AddRemove方法引用this.InnerList ,而索引器引用this.List

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM