Dictionary.add给我一个复制的最后一个对象添加了c# [英] Dictionary.add gives me a replicate of the last object added c#

查看:253
本文介绍了Dictionary.add给我一个复制的最后一个对象添加了c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据添加到我的字典中,但最后我发现我只有一个对象的副本;这是我的代码

 (!rst.EOF)
{
数据d =读取(rst)作为数据;
MapToObject.Add(d.Id,d);
rst.MoveNext();
}





最后,MapToObject包含最后添加数据的副本。我该怎么办才能修复它?

- MapToObject只是一个字典int,对象

-Read(rst)只是一个从第一个获取elemet并将它存储到d
的函数
-rst只是一个数据库记录



我尝试过:



d =读取(rst)作为数据;

MapToObject.Add(d.Id,new object());

MapToObject [d.Id] = d;

解决方案
的方法,字典< TKEY的,TValue>。新增(TKEY的键,TValue值)会如果试图插入第二个相同的密钥抛出异常(而通过索引器属性( MapToObject [d.Id] = d; )设置一个值只会用相同的密钥覆盖现有条目。



因此,如果你没有得到异常并且你的词典中只有一个条目,那么你的while循环只运行一次 rst.EOF 成为现实。



如果您有多个条目,那么至少密钥必须不同。在这种情况下,您可能实际上没有指定 d 作为要插入的值,而是 new object() like你已经在我尝试过的东西下显示了。


字典< int,object> class不能存储重复的键值 - 如果尝试,则抛出Add方法和异常。

因此它不包含重复 - Dictionary中每条记录的整数值必须不同一。并且,由于您从要添加的Data类的实例中获取此值,因此您添加的每个实例中的ID值都是不同的。

但是,这并不意味着您不会重复添加相同的值并在Read方法中覆盖它的Id和其他属性。

我会从检查创建新实例开始每次调用它而不是回收单个实例,并使用调试器准确计算出发生的情况,如果不明显的那样。


I'm trying to add data to my dictionary, but at the end I find I only have replicas of a single object; here is my code

while (!rst.EOF)
{        
    Data d = Read(rst) as Data;
    MapToObject.Add(d.Id, d);
    rst.MoveNext();
}



At the end MapToObject contains a replica of the last data added. What can I do to fix it?
-the MapToObject is just a dictionary int,object
-Read(rst) is just a function that takes the elemet from rst and stor it to d
-rst is just a database record

What I have tried:

d = Read(rst) as Data;
MapToObject.Add(d.Id,new object());
MapToObject[d.Id] = d;

解决方案

The method Dictionary<TKey,TValue>.Add(TKey key, TValue value) will throw an exception if you try to insert a second identical key (whereas setting a value via the indexer property (MapToObject[d.Id] = d;) will simply overwrite an existing entry with the same key).

So if you do not get an exception and you end up with only one entry in your Dictionary then your while-loop only ran once before rst.EOF became true.

If you do have more than one entry then at least the keys have to be different. In that case you probably didn't actually specify d as the value to be inserted but instead new object() like you've shown under "What I have tried".


The Dictionary<int, object> class can't store duplicate key values - the Add method throws and exception if you try.
So it isn't containing a duplicate - the integer value for each record in the Dictionary must be different for each one. And since you get this value from the instance of the Data class you are adding, it stands to reason that the ID value is different in each instance you add, at the point at which you add it.
However, that doesn't mean that you aren't adding the same value repeatedly and overwriting it's Id and other properties in your Read method.
I'd start there by checking that it creates a new instance each time it's called rather than "recycling" a single instance, and use the debugger to work out exactly what is happening if it wasn't obvious from that.


这篇关于Dictionary.add给我一个复制的最后一个对象添加了c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆