implementing IList for a sortedlist
-
I looking to have a sortedlist that I can bind to form controls. I can write a class that extends SortedList and implements the IList interface. My first question is the syntax for extending a class and implementing an interface are the same. How does the compiler know to differeniate between the two. My other question is since the SortedList class already implments many of the methods required for the IList interface (e.g. Add, Clear, Contains, etc...) do I have to overwrite these methods?
-
I looking to have a sortedlist that I can bind to form controls. I can write a class that extends SortedList and implements the IList interface. My first question is the syntax for extending a class and implementing an interface are the same. How does the compiler know to differeniate between the two. My other question is since the SortedList class already implments many of the methods required for the IList interface (e.g. Add, Clear, Contains, etc...) do I have to overwrite these methods?
The compiler knows because the names match up to a single definition. If there are multiple methods with the same signature (based on name and param types) in various interfaces (for which a class can implement many) or in the base class (for which a class can extend only one), then you must use explicit interface methods (which answers another question):
public class MyClass : BaseClass, IMyInterface
{
// In this example, both BaseClass and IMyInterface have a method called
// Foo
public override void Foo()
{
DoSomething();
}
// Notice that there's no access modifier below.
void IMyInterface.Foo()
{
DoSomethingElse();
}
}This is really only necessary when the returns types differ or when you want a method based on one class or interface to do something different when from another interface. Many times, just let both
BaseClass.Foo
andIMyInterface.Foo
refer to the same code (which the compiler handles just fine). Properties are probably benefited from this the most.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
The compiler knows because the names match up to a single definition. If there are multiple methods with the same signature (based on name and param types) in various interfaces (for which a class can implement many) or in the base class (for which a class can extend only one), then you must use explicit interface methods (which answers another question):
public class MyClass : BaseClass, IMyInterface
{
// In this example, both BaseClass and IMyInterface have a method called
// Foo
public override void Foo()
{
DoSomething();
}
// Notice that there's no access modifier below.
void IMyInterface.Foo()
{
DoSomethingElse();
}
}This is really only necessary when the returns types differ or when you want a method based on one class or interface to do something different when from another interface. Many times, just let both
BaseClass.Foo
andIMyInterface.Foo
refer to the same code (which the compiler handles just fine). Properties are probably benefited from this the most.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
So if I'm understanding you correctly. If I write a class and extend SortedList I get all the functionality of a SortedList and implement IList for databinding by doing the following: public abstract class IndexSortedList: SortedList, IList //Does order matter here? { //This is the only IList method not implemented by SortedList public void Insert() { DoSomething(); } //Do I have to include this method as well and just simply //tell it to call the Add method from the SortedList? //This method is already implented in SortedList, but required by IList. public override void Add() { //call SortedList's Add method base.Add(); } } Can you confirm the questions in my comments? Thanks!
-
So if I'm understanding you correctly. If I write a class and extend SortedList I get all the functionality of a SortedList and implement IList for databinding by doing the following: public abstract class IndexSortedList: SortedList, IList //Does order matter here? { //This is the only IList method not implemented by SortedList public void Insert() { DoSomething(); } //Do I have to include this method as well and just simply //tell it to call the Add method from the SortedList? //This method is already implented in SortedList, but required by IList. public override void Add() { //call SortedList's Add method base.Add(); } } Can you confirm the questions in my comments? Thanks!
If the
SortedList
alredy implements anAdd
method with the right signature, you don't need to do anything! It's already done for you. An interface is just a contract that ensures that an implementing class has members defined in the interface. Your class already inherits anAdd
method (as well as many others) so you don't need to do anything. Just implementInsert
and call it good.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
If the
SortedList
alredy implements anAdd
method with the right signature, you don't need to do anything! It's already done for you. An interface is just a contract that ensures that an implementing class has members defined in the interface. Your class already inherits anAdd
method (as well as many others) so you don't need to do anything. Just implementInsert
and call it good.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----