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. Explicit interface implementation

Explicit interface implementation

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    When I implement an interface explicitly like this int IComparable.CompareTo(byte other) { ... } I am unable to call it my self from another class, it does not show up in intellisence. Is this normal, if so why is it like this? I know that when I implement an interface like this is becomes explicitly public but I still cannot call it. This is for a value type also (I dont know if that makes any difference or not).

    █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

    B D 2 Replies Last reply
    0
    • L Lost User

      When I implement an interface explicitly like this int IComparable.CompareTo(byte other) { ... } I am unable to call it my self from another class, it does not show up in intellisence. Is this normal, if so why is it like this? I know that when I implement an interface like this is becomes explicitly public but I still cannot call it. This is for a value type also (I dont know if that makes any difference or not).

      █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

      B Offline
      B Offline
      ben2
      wrote on last edited by
      #2

      did you try something like: class A:IComparable { } ______________________________________ A a = new A(); IComparable ia = a as IComparable; if (ia!=null) ia.ComapreTo(...); I don't know if it'll work...you can try it

      1 Reply Last reply
      0
      • L Lost User

        When I implement an interface explicitly like this int IComparable.CompareTo(byte other) { ... } I am unable to call it my self from another class, it does not show up in intellisence. Is this normal, if so why is it like this? I know that when I implement an interface like this is becomes explicitly public but I still cannot call it. This is for a value type also (I dont know if that makes any difference or not).

        █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

        D Offline
        D Offline
        Daniel Grunwald
        wrote on last edited by
        #3

        Explicit interface implementation allows you to implement an interface without adding public methods to your class. You will be able to call the method by casting an object instance to the interface type. ((IComparable)myInstance).CompareTo(other) This is mainly used in two cases: - there is another public method that does the same job and should be called instead (FileStream.Close vs IDisposable.Dispose) - the method is expected to be only called by some other code that always operates on the interface and doesn't know about your class, and you don't want that the methods show when using your object. (often used with ICustomTypeDescriptor)

        L 1 Reply Last reply
        0
        • D Daniel Grunwald

          Explicit interface implementation allows you to implement an interface without adding public methods to your class. You will be able to call the method by casting an object instance to the interface type. ((IComparable)myInstance).CompareTo(other) This is mainly used in two cases: - there is another public method that does the same job and should be called instead (FileStream.Close vs IDisposable.Dispose) - the method is expected to be only called by some other code that always operates on the interface and doesn't know about your class, and you don't want that the methods show when using your object. (often used with ICustomTypeDescriptor)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Daniel Grunwald wrote:

          You will be able to call the method by casting an object instance to the interface type. ((IComparable)myInstance).CompareTo(other)

          I remember now. I did this last night... IComparable test = this; test.[Intellisence found it]; It compiled without errors. Thanks for the information.

          █▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██

          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