1
长话短说,我需要一组具有字典式功能的对象,可以序列化以保存用户数据。原始词典是一个Dictionary类,它包含一个Item对象数组和每个对象由用户“持有”的数量。在互联网上找到一些建议后,我试着从KeyedCollection中实现我自己的类似字典的类,但似乎无法向它添加对象。我是否添加对象错误或者与我的收藏有关?尝试实现来自Keyedcollection的Serializble DIctionary,无法添加对象
在 'SerialDictionary' 类:
public class SerialDictionary : KeyedCollection<Item, int>
{
protected override int GetKeyForItem(Item target)
{
return target.Key;
}
}
public class Item
{
private int index;
private string attribute;
public Item(int i, string a)
{
index = i;
attribute = a;
}
public int Key
{
get { return index; }
set { index = value; }
}
public string Attribute
{
get { return attribute; }
set { attribute = value; }
}
}
的主要形式(即试图添加的对象)
public partial class Form1 : Form
{
SerialDictionary ItemList;
Item orb;
public Form1()
{
InitializeComponent();
ItemList = new SerialDictionary();
orb = new Item(0001, "It wants your lunch!");
orb.Key = 001;
}
private void button1_Click(object sender, EventArgs e)
{
ItemList.Add(orb);
}
}
错误我试图添加对象时接收:
'System.Collections.ObjectModel.Collection.Add(int)'的最佳重载方法匹配有一些无效参数
如果我在那里编译抛出一个int,但我想在那里得到的项目对象的集合......
索引有可能是除int以外的任何东西吗?我最初使用Item类作为索引,但如果需要,我可以通过实用的方式进行更改。 – ChargerIIC 2012-02-25 03:35:23