Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. implementing IList for a sortedlist

implementing IList for a sortedlist

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zuhx
    wrote on last edited by
    #1

    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?

    H 1 Reply Last reply
    0
    • Z zuhx

      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?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 and IMyInterface.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-----

      Z 1 Reply Last reply
      0
      • H Heath Stewart

        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 and IMyInterface.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-----

        Z Offline
        Z Offline
        zuhx
        wrote on last edited by
        #3

        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!

        H 1 Reply Last reply
        0
        • Z zuhx

          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!

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          If the SortedList alredy implements an Add 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 an Add method (as well as many others) so you don't need to do anything. Just implement Insert 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-----

          Z 1 Reply Last reply
          0
          • H Heath Stewart

            If the SortedList alredy implements an Add 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 an Add method (as well as many others) so you don't need to do anything. Just implement Insert 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-----

            Z Offline
            Z Offline
            zuhx
            wrote on last edited by
            #5

            Thanks! Much appreciated. I'm gonna give it a try later this afternoon.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups