From: Arne Vajhøj on 28 Jun 2010 15:18 On 28-06-2010 14:20, Rich P wrote: > I found > > public class SimpleDictionary : IDictionary > > for creating a custom collection object at this site: > > http://msdn.microsoft.com/en-us/library/system.collections.idictionary.aspx > > The following is a simple class called MyClass (followed by > SimpleDictionary : IDictionary). I can add MyClass objects to > SimpleDictionary as is. But if I add the following members to > IDictionary<int, MyClass> -- then my test app complains that I am not > implementing the members. How do I emplement these members<int, > MyClass> ? You are not making it easy for us - since you are not providing the exact error message. But a quick glance at the code indicates that some of the methods implementing the interfaces need some generic types. public ICollection<int> Keys public ICollection<MyClass> Values IEnumerator<int> IEnumerable.GetEnumerator() Arne
From: Arne Vajhøj on 28 Jun 2010 17:38 On 28-06-2010 16:36, Rich P wrote: > Thank you for your reply. I made a few changes (like class name to > MyClassCollection. I also applied your suggestions above. I made > everything that said key and<int> and everything that said Value > <MyClass>. I am getting 17 errors. Following are 3 of the errors (one > error is common to about 10 of the errors, the 3rd error is self > explanatory) and a relisting of my collection class with the changes > that I have made thus far (note: the whole point of this exercise is to > learn how to implement interfaces and interfaces with members): > > Error 1 -- 'InterfaceDictionary.MyClassCollection' does not implement > interfacemember 'System.Collections.IEnumerable.GetEnumerator()'. > 'InterfaceDictionary.MyClassCollection.GetEnumerator()' cannot implement > 'System.Collections.IEnumerable.GetEnumerator()' because it does not > have the matching return type of 'System.Collections.IEnumerator'. > Error 2 -- 'InterfaceDictionary.MyClassCollection' does not implement > interface member > 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyVa > luePair<int,InterfaceDictionary.MyClass>>.GetEnumerator()'. > 'InterfaceDictionary.MyClassCollection.GetEnumerator()' cannot implement > 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyVa > luePair<int,InterfaceDictionary.MyClass>>.GetEnumerator()' because it > does not have the matching return type of > 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyVa > luePair<int,InterfaceDictionary.MyClass>>'. > > Error 11 -- 'IEnumerable.GetEnumerator' in explicit interface > declaration is not a member of interface The problem must be with those two: > public IDictionaryEnumerator GetEnumerator() > { > // Construct and return an enumerator. > return new MyClassEnumerator(this); > } > IEnumerator<int> IEnumerable.GetEnumerator() > { > // Construct and return an enumerator. > return ((IDictionary)this).GetEnumerator(); > } They seems to have wrong return types compared to the interfaces. The easiest would probbaly be to ask your IDE to add the missing methods and see the proper signature. Arne
From: Arne Vajhøj on 28 Jun 2010 17:44 On 28-06-2010 17:38, Arne Vajh�j wrote: > On 28-06-2010 16:36, Rich P wrote: >> Thank you for your reply. I made a few changes (like class name to >> MyClassCollection. I also applied your suggestions above. I made >> everything that said key and<int> and everything that said Value >> <MyClass>. I am getting 17 errors. Following are 3 of the errors (one >> error is common to about 10 of the errors, the 3rd error is self >> explanatory) and a relisting of my collection class with the changes >> that I have made thus far (note: the whole point of this exercise is to >> learn how to implement interfaces and interfaces with members): >> >> Error 1 -- 'InterfaceDictionary.MyClassCollection' does not implement >> interfacemember 'System.Collections.IEnumerable.GetEnumerator()'. >> 'InterfaceDictionary.MyClassCollection.GetEnumerator()' cannot implement >> 'System.Collections.IEnumerable.GetEnumerator()' because it does not >> have the matching return type of 'System.Collections.IEnumerator'. > >> Error 2 -- 'InterfaceDictionary.MyClassCollection' does not implement >> interface member >> 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyVa >> luePair<int,InterfaceDictionary.MyClass>>.GetEnumerator()'. >> 'InterfaceDictionary.MyClassCollection.GetEnumerator()' cannot implement >> 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyVa >> luePair<int,InterfaceDictionary.MyClass>>.GetEnumerator()' because it >> does not have the matching return type of >> 'System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyVa >> luePair<int,InterfaceDictionary.MyClass>>'. >> >> Error 11 -- 'IEnumerable.GetEnumerator' in explicit interface >> declaration is not a member of interface > > The problem must be with those two: > >> public IDictionaryEnumerator GetEnumerator() >> { >> // Construct and return an enumerator. >> return new MyClassEnumerator(this); >> } > >> IEnumerator<int> IEnumerable.GetEnumerator() >> { >> // Construct and return an enumerator. >> return ((IDictionary)this).GetEnumerator(); >> } > > They seems to have wrong return types compared to > the interfaces. > > The easiest would probbaly be to ask your IDE to add the > missing methods and see the proper signature. I just did. They are supposed to be: public IEnumerator<KeyValuePair<int, MyClass>> GetEnumerator() IEnumerator IEnumerable.GetEnumerator() Arne
From: Arne Vajhøj on 28 Jun 2010 18:07 On 28-06-2010 17:50, Rich P wrote: >> They seems to have wrong return types compared to >> the interfaces. >> >> The easiest would probbaly be to ask your IDE to add the >> missing methods and see the proper signature. > I'm using VS2008 Pro. How would I go about asking the IDE to add the > missing methods? Right click and chose "implement interface - implement interface". Edit the method bodies but keep the signatures as is. Arne
|
Pages: 1 Prev: Task manager in C# Next: VWD 2010 Express calls Win32 DLL |