[英]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
. 代码中的另一个奇怪之处是
Add
和Remove
方法引用this.InnerList
,而索引器引用this.List
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.