简体   繁体   English

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

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

I an getting an exception "Index was out of range. Must be non-negative and less than the size of the collection" in the following code.Here in the code actually what going on is , handling some duplicate values which is finally occupying in a data grid 我在以下代码中遇到一个异常:“索引超出范围。必须为非负数,并且小于集合的大小。”代码中实际上是怎么回事,处理了一些重复的值,这些值最终被占用数据网格

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

            }
         }

Do i need to increase the size of types like int to long ? 我需要将int等类型的大小增加到long吗? or any additional check in code is required ? 还是需要任何其他签入代码?

Since you are using a lock statement, this is presumably a multi-threaded implementation. 由于您使用的是lock语句,因此大概是多线程实现。

A likely cause is that you are failing to synchronize access to your object properly. 可能的原因是您无法正确同步对对象的访问。 Look at any other code that updates the collection ( this in your code above) - post it if the problem isn't obvious. 再看那将更新集合任何其他代码( this在上面的代码) -如果这个问题并不明显张贴。

UPDATE 更新

For example, in your updated source code, the indexer's setter is not synchronized: 例如,在更新的源代码中,索引器的设置器未同步:

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

You probably need the following: 您可能需要以下内容:

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

Another oddity in your code is that the Add and Remove methods reference this.InnerList , while the indexer references this.List . 代码中的另一个奇怪之处是AddRemove方法引用this.InnerList ,而索引器引用this.List

暂无
暂无

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

相关问题 System.ArgumentOutOfRangeException:'索引超出范围。 必须为非负数,并且小于集合的大小。” - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.' System.ArgumentOutOfRangeException: '索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引' - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' System.ArgumentOutOfRangeException:索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ArgumentOutOfRangeException 索引超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index ArgumentOutOfRangeException-索引超出范围。 必须为非负数且小于集合的大小 - ArgumentOutOfRangeException - index was out of range. Must be non-negative and less than the size of the collection 错误C#:索引超出范围。 必须为非负数且小于集合的大小 - Error C#: Index was out of range. Must be non-negative and less than the size of the collection 索引超出范围。 必须为非负数并且小于集合错误的大小 - Index was out of range. Must be non-negative and less than the size of the collection error 索引超出范围。 必须为非负数并且小于Nhibernate中的集合错误大小 - Index was out of range. Must be non-negative and less than the size of the collection error in Nhibernate excpetion error:索引超出范围。 必须是非负数且小于集合的大小 - excpetion error :Index was out of range. Must be non-negative and less than the size of the collection 错误索引超出范围。 必须是非负数且小于集合的大小 - Error Index was out of range. Must be non-negative and less than the size of the collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM