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. How to access the explicit interface implementation of a base class from a dervied class ?

How to access the explicit interface implementation of a base class from a dervied class ?

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 4 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.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.

    public interface ISomeInterface {

    void SomeMethode();
    

    }

    public class BaseClass : ISomeInterface {

    object SomeInterface.SomeMethode{
        ...
    }
    

    }

    public class DerivedClass : BaseClass, ISomeInterface {

    object SomeInterface.SomeMethode{
        ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context
    }
    

    }

    F J D 3 Replies Last reply
    0
    • M MarkPhB

      Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.

      public interface ISomeInterface {

      void SomeMethode();
      

      }

      public class BaseClass : ISomeInterface {

      object SomeInterface.SomeMethode{
          ...
      }
      

      }

      public class DerivedClass : BaseClass, ISomeInterface {

      object SomeInterface.SomeMethode{
          ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context
      }
      

      }

      F Offline
      F Offline
      Frank Horn
      wrote on last edited by
      #2

      Why do you declare public class DerivedClass : BaseClass, ISomeInterface at all? Shouldn't public class DerivedClass : BaseClass be enough to ensure that DerivedClass implements ISomeInterface?

      M 1 Reply Last reply
      0
      • F Frank Horn

        Why do you declare public class DerivedClass : BaseClass, ISomeInterface at all? Shouldn't public class DerivedClass : BaseClass be enough to ensure that DerivedClass implements ISomeInterface?

        M Offline
        M Offline
        MarkPhB
        wrote on last edited by
        #3

        Because i couldn't implement the interface explicit ;)

        J 1 Reply Last reply
        0
        • M MarkPhB

          Because i couldn't implement the interface explicit ;)

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          MarkPhB wrote:

          Because i couldn't implement the interface explicit

          You need to explain what you mean by this. Implementing an interface explicitly is done for a reason, you might not have wanted to implement the interface explicitly in the first place!

          1 Reply Last reply
          0
          • M MarkPhB

            Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.

            public interface ISomeInterface {

            void SomeMethode();
            

            }

            public class BaseClass : ISomeInterface {

            object SomeInterface.SomeMethode{
                ...
            }
            

            }

            public class DerivedClass : BaseClass, ISomeInterface {

            object SomeInterface.SomeMethode{
                ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context
            }
            

            }

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            As ive said above its not entirely clear what you are trying to achieve. However, you can access that explicit interface in the derived class without also implementing the interface.

            public interface ISomeInterface{
            void SomeMethod();
            }

            public class BaseClass : ISomeInterface {
            void SomeInterface.SomeMethode(){
            ...
            }
            }

            public class DerivedClass : BaseClass{
            void AnotherMethod(){
            //This wont work, its implemented explicitly
            //this.SomeMethod()
            //This will work
            ((ISomeInterface)this).SomeMethod();
            }
            }

            Incidentally if you change BaseClass too the following the commented out line above will work. This is implementing the interface implicitly

            public class BaseClass : ISomeInterface {
            public void SomeMethode(){
            ...
            }
            }

            1 Reply Last reply
            0
            • M MarkPhB

              Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.

              public interface ISomeInterface {

              void SomeMethode();
              

              }

              public class BaseClass : ISomeInterface {

              object SomeInterface.SomeMethode{
                  ...
              }
              

              }

              public class DerivedClass : BaseClass, ISomeInterface {

              object SomeInterface.SomeMethode{
                  ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context
              }
              

              }

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              J4amieC has given you the correct answer above. It's the difference between explicit and implicit. If you want to change a value from one type to another in code then if an implicit operator exists you can just set one to equal the other. If not, you have to explicitly cast to the type you require. The same applies to interfaces. If you implement them explicitly then the cast is required in any classes that derive from the class where the implementation was made. ((ISomeInterface)this).SomeMethode();

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Expect everything to be hard and then enjoy the things that come easy. (code-frog)

              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