Here is my issue: It doesn't seem like I can use the .Dictionary property or the GetKeyForItem method. Is there a using statement that I'm missing or something? Essentially, I'd like to retrieve a list of keys for each object in my keyedcollection. I've found another way to do it (shown in the code), but I'm just wondering why the built in .Dictionary and .GetKeyForItem aren't working. I'm thinking that if I can't access these, maybe I set something up incorrectly? Thanks for the help.
namespace testList
{
public class MyData //data itself has to contain the key.
{
public int Id;
public List<string> Data;
}
public class MyKeyedCollection : KeyedCollection<int, MyData>
{
//was initially protected. Changed to public. Still can't access this?
public override int GetKeyForItem(MyData item)
{
return item.Id;
}
}
class Program
{
static void Main(string[] args)
{
MyData nd = new MyData();
MyData md = new MyData();
nd.Id = 2;
nd.Data = new List<string>();
nd.Data.Add("Random Item");
md.Id =1;
md.Data = new List<string>();
md.Data.Add("First Item");
md.Data.Add("Second item");
KeyedCollection<int, MyData> keyd = new MyKeyedCollection();
keyd.Add(nd);
keyd.Add(md);
// doesn't recognize keyd.GetKeyForItem
//Since the mentioned methods aren't working, my only other solution is this:
/*
int[] keys = new int[keyd.Count];
for(int i=0; i<keyd.Count; i++)
{
keys[i] = keyd[i].Id;
}
*/
}
}
}
Sources: