From: Rich P on 1 Jul 2010 12:46 I am attempting to create a custom collection class which implements IDictionary -- with parameters -- public class MyClassCollection : IDictionary <int, MyClass> The purpose for this effort is to learn C# programmer at a slightly deeper level. I have a working sample that does not include parameters. When I add the parameters <int, MyClass> this is when I get errors. At this time -- my collection class -- with the parameters -- WILL hold a collection of custom class objects. Right now -- my collection class can store my custom class objects, and I can retrieve a count of objects being store. My problem is iterating through the collection class. I added a private class for iterating, and that caused 2 errors to pop up. If I remove the private class - the errors go away. Currently, I don't have enough background with Interfaces and am having diffuculty proceeding. Here are the 2 error messages I am getting with what I have so far, and below that is the code: Error 1 'InterfaceDictionary.MyClassCollection.MyClassCollectionEnumerator' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()' Error 2 'InterfaceDictionary.MyClassCollection.MyClassCollectionEnumerator' does not implement interface member 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyVa luePair<int,InterfaceDictionary.MyClass>>.GetEnumerator()' here is my code -- how can I make this work? ---------------------------------------------------- class Program { static void Main(string[] args) { MyClass e1, e2; e1 = new MyClass(1001, "Andy Reid"); e2 = new MyClass(1002, "Kara Lang"); MyClassCollection eData = new MyClassCollection(2); eData.Add(e1.EmpID, e1); eData.Add(e2.EmpID, e1); Console.WriteLine(eData.Count.ToString()); foreach (KeyValuePair<int, MyClass> de in eData) { Console.WriteLine(de.Key.ToString() + " " + de.Value.ToString()); } } } public class MyClass { public int EmpID; public string EmpName; public MyClass(int i, string s) { EmpID = i; EmpName = s; } } public class MyClassCollection : IDictionary <int, MyClass> { // The array of items private DictionaryEntry[] items; private Int32 ItemsInUse = 0; private Int32 Idx = 0; //// Construct the SimpleDictionary with the desired number of items. //// The number of items cannot change for the life time of this SimpleDictionary. public MyClassCollection(Int32 numItems) { items = new DictionaryEntry[numItems]; } #region IDictionary<int,MyClass> Members public void Add(int key, MyClass value) { if (ItemsInUse == items.Length) throw new InvalidOperationException("The dictionary cannot hold any more items."); items[ItemsInUse++] = new DictionaryEntry(key, value); //throw new NotImplementedException(); } public bool ContainsKey(int key) { throw new NotImplementedException(); } public ICollection<int> Keys { get { throw new NotImplementedException(); } } public bool Remove(int key) { throw new NotImplementedException(); } public bool TryGetValue(int key, out MyClass value) { throw new NotImplementedException(); } private class MyClassCollectionEnumerator : IEnumerable<KeyValuePair<int,MyClass>> { //how/where do I implement the elements listed in the error messages? // implementation of constructor, IEnmerator members, etc. here public MyClassCollectionEnumerator(MyClassCollection mc) { } } public ICollection<MyClass> Values { get { throw new NotImplementedException(); } } public MyClass this[int key] { get { return this[key]; ; throw new NotImplementedException(); } set { throw new NotImplementedException(); } } #endregion #region ICollection<KeyValuePair<int,MyClass>> Members public void Add(KeyValuePair<int, MyClass> item) { throw new NotImplementedException(); } public void Clear() { throw new NotImplementedException(); } public bool Contains(KeyValuePair<int, MyClass> item) { throw new NotImplementedException(); } public void CopyTo(KeyValuePair<int, MyClass>[] array, int arrayIndex) { throw new NotImplementedException(); } public int Count { get { return ItemsInUse; } } public bool IsReadOnly { get { throw new NotImplementedException(); } } public bool Remove(KeyValuePair<int, MyClass> item) { throw new NotImplementedException(); } #endregion #region IEnumerable<KeyValuePair<int,MyClass>> Members public IEnumerator<KeyValuePair<int, MyClass>> GetEnumerator() { //how do I return MyClassCollectionEnumerator ? //return MyClassCollectionEnumerator...; throw new NotImplementedException(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } #endregion } Thanks Rich *** Sent via Developersdex http://www.developersdex.com ***
From: Rich P on 1 Jul 2010 16:08 Amazing! Thank you for your reply. Working now. As for why I am even doing this -- it is all a learning process. Over the years, I have consistenly only been able to learning something "the hard way". If it doesn't hurt and my head doesn't feel like it is going to come off my shoulders - I either won't learn the thing or the thing wasn't worth learning. With this effort alone, I have learned quite a bit about interfaces. And I HAVE read up on the subject. I kinda know the difference between an Interface and an Abstract class. I guess what I am really learning here -- is the IDE and what it can do (VS2008 in my case). Thank you again for your help and thanks to all who have offered suggestions for this effort. Rich *** Sent via Developersdex http://www.developersdex.com ***
|
Pages: 1 Prev: Call stored procedure with paremeter output in c# Next: Console App Display DLL logging info |