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. interfaces

interfaces

Scheduled Pinned Locked Moved C#
question
7 Posts 5 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.
  • K Offline
    K Offline
    kalyan_2416
    wrote on last edited by
    #1

    i have two interfaces i1 and i2; both of them have got a function with the same name i1- function_some(); i2- function_some(); now i am gonna implement both the interfaces to a class how do i define the functions after implementing.... of i1 and i2 respectively thanks bye

    C 1 Reply Last reply
    0
    • K kalyan_2416

      i have two interfaces i1 and i2; both of them have got a function with the same name i1- function_some(); i2- function_some(); now i am gonna implement both the interfaces to a class how do i define the functions after implementing.... of i1 and i2 respectively thanks bye

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You will scope them explicitly. void i1.function_some() { // body } void i2.function_some() { // body } Now, if you try to call function_some on your class instance, you'll get an error, you'll need to scope it there, too, I am not sure if there's a way to do this, apart from casting to the right interface.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      K V 2 Replies Last reply
      0
      • C Christian Graus

        You will scope them explicitly. void i1.function_some() { // body } void i2.function_some() { // body } Now, if you try to call function_some on your class instance, you'll get an error, you'll need to scope it there, too, I am not sure if there's a way to do this, apart from casting to the right interface.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        K Offline
        K Offline
        kalyan_2416
        wrote on last edited by
        #3

        class c { void i1.function_some() { // body } void i2.function_some() { // body } } class c1:c { c dd =new c(); dd.//how do i call particular funcion } if i inherit the class where i implemented how do i call these in the inherited class

        L 1 Reply Last reply
        0
        • K kalyan_2416

          class c { void i1.function_some() { // body } void i2.function_some() { // body } } class c1:c { c dd =new c(); dd.//how do i call particular funcion } if i inherit the class where i implemented how do i call these in the inherited class

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          As already mentioned - cast it to the correct interface: ((i2)dd).function_some() If you need to do this your OO design is most likely wrong though.

          K 1 Reply Last reply
          0
          • L lmoelleb

            As already mentioned - cast it to the correct interface: ((i2)dd).function_some() If you need to do this your OO design is most likely wrong though.

            K Offline
            K Offline
            kalyan_2416
            wrote on last edited by
            #5

            thnx i have used things in the similar manner but it didnt strike me thanx

            1 Reply Last reply
            0
            • C Christian Graus

              You will scope them explicitly. void i1.function_some() { // body } void i2.function_some() { // body } Now, if you try to call function_some on your class instance, you'll get an error, you'll need to scope it there, too, I am not sure if there's a way to do this, apart from casting to the right interface.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              V Offline
              V Offline
              Vikram A Punathambekar
              wrote on last edited by
              #6

              Christian Graus wrote:

              You will scope them explicitly.

              Not necessarily:

              interface I1
              {
              void TheMethod();
              }

              interface I2
              {
              void TheMethod();
              }

              class C : I1, I2
              {
              public void TheMethod()
              {
              Console.WriteLine("Did you ask for me?");
              }
              }

              public static int Main(string[] args)
              {
              C c = new C();
              I1 i1 = new C();
              I2 i2 = new C();
              c.TheMethod();
              i1.TheMethod();
              i2.TheMethod();
              return 0;
              }

              However, I agree that it's bad design.

              Cheers, Vıkram.


              After all is said and done, much is said and little is done.

              P 1 Reply Last reply
              0
              • V Vikram A Punathambekar

                Christian Graus wrote:

                You will scope them explicitly.

                Not necessarily:

                interface I1
                {
                void TheMethod();
                }

                interface I2
                {
                void TheMethod();
                }

                class C : I1, I2
                {
                public void TheMethod()
                {
                Console.WriteLine("Did you ask for me?");
                }
                }

                public static int Main(string[] args)
                {
                C c = new C();
                I1 i1 = new C();
                I2 i2 = new C();
                c.TheMethod();
                i1.TheMethod();
                i2.TheMethod();
                return 0;
                }

                However, I agree that it's bad design.

                Cheers, Vıkram.


                After all is said and done, much is said and little is done.

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                Right, if the contracts of both interfaces can be satisfied with one method, then things are simple. Until I read this thread I didn't know any other way existed. A class that implements more than one interface may need to provide a separate implementation for each.

                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