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. A typical case about interfaces [modified]

A typical case about interfaces [modified]

Scheduled Pinned Locked Moved C#
helpcsharp
11 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.
  • A Offline
    A Offline
    amitcoder83
    wrote on last edited by
    #1

    the following code is in C# I have 2 interfaces public interface Interface1 { string ReturnString1(); } public interface Interface2 { string ReturnString1(); } now i implement both the interfaces in a single class Class A:Interface1,Interface2 { } now according to my searchings i found that we can implement both the interfaces in the following manner Class A:Interface1,Interface2 { public string Interface1.ReturnString1() { return "Interface1.ReturnString1"; } public string Interface2.ReturnString1() { return "Interface2.ReturnString1"; } } but on compiling it is giving an error:"the modifier public is not valid for this item" after i remove public and then i compile the code, it compiles successfully but i'm not able to access these methods by creating an object of the class. Can anyone solve this problem Amit More (CMC) -- modified at 3:01 Wednesday 6th June, 2007 Amit More (CMC)

    C M 2 Replies Last reply
    0
    • A amitcoder83

      the following code is in C# I have 2 interfaces public interface Interface1 { string ReturnString1(); } public interface Interface2 { string ReturnString1(); } now i implement both the interfaces in a single class Class A:Interface1,Interface2 { } now according to my searchings i found that we can implement both the interfaces in the following manner Class A:Interface1,Interface2 { public string Interface1.ReturnString1() { return "Interface1.ReturnString1"; } public string Interface2.ReturnString1() { return "Interface2.ReturnString1"; } } but on compiling it is giving an error:"the modifier public is not valid for this item" after i remove public and then i compile the code, it compiles successfully but i'm not able to access these methods by creating an object of the class. Can anyone solve this problem Amit More (CMC) -- modified at 3:01 Wednesday 6th June, 2007 Amit More (CMC)

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

      amitcoder83 wrote:

      string ReturnString1();

      the default access for any method or property is private. If you remove 'public' then they default to private right through your code.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "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 )

      M O 2 Replies Last reply
      0
      • A amitcoder83

        the following code is in C# I have 2 interfaces public interface Interface1 { string ReturnString1(); } public interface Interface2 { string ReturnString1(); } now i implement both the interfaces in a single class Class A:Interface1,Interface2 { } now according to my searchings i found that we can implement both the interfaces in the following manner Class A:Interface1,Interface2 { public string Interface1.ReturnString1() { return "Interface1.ReturnString1"; } public string Interface2.ReturnString1() { return "Interface2.ReturnString1"; } } but on compiling it is giving an error:"the modifier public is not valid for this item" after i remove public and then i compile the code, it compiles successfully but i'm not able to access these methods by creating an object of the class. Can anyone solve this problem Amit More (CMC) -- modified at 3:01 Wednesday 6th June, 2007 Amit More (CMC)

        M Offline
        M Offline
        Manoj Kumar Rai
        wrote on last edited by
        #3

        Its correct that modifier public is not valid for those function. Also pls make sure you are accessing those function from a object type of Interface1 or Interface2 and not from a object of the class A directly, to get the access to those function. i.e. Interface1 interface1 = new A(); interface1.ReturnString1(); //Here you will be able to access the function.

        Manoj Never Gives up

        A 1 Reply Last reply
        0
        • C Christian Graus

          amitcoder83 wrote:

          string ReturnString1();

          the default access for any method or property is private. If you remove 'public' then they default to private right through your code.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "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 )

          M Offline
          M Offline
          Manoj Kumar Rai
          wrote on last edited by
          #4

          I think you are not correct:-)

          Manoj Never Gives up

          O 1 Reply Last reply
          0
          • M Manoj Kumar Rai

            Its correct that modifier public is not valid for those function. Also pls make sure you are accessing those function from a object type of Interface1 or Interface2 and not from a object of the class A directly, to get the access to those function. i.e. Interface1 interface1 = new A(); interface1.ReturnString1(); //Here you will be able to access the function.

            Manoj Never Gives up

            A Offline
            A Offline
            amitcoder83
            wrote on last edited by
            #5

            All the code is in C#, so if i dont specify access modifier then by default it is public after implementing the methods in an interface we can access the methods thru that class's object.why can't we do the same with this. Amit More(CMC) -- modified at 3:10 Wednesday 6th June, 2007

            M 1 Reply Last reply
            0
            • A amitcoder83

              All the code is in C#, so if i dont specify access modifier then by default it is public after implementing the methods in an interface we can access the methods thru that class's object.why can't we do the same with this. Amit More(CMC) -- modified at 3:10 Wednesday 6th June, 2007

              M Offline
              M Offline
              Manoj Kumar Rai
              wrote on last edited by
              #6

              See the function name in the class A is as Interface1.ReturnString1(), and thats why you can not a.ReturnString1(). But the function name for the Interface1 is "ReturnString1" and you can call it using object of type Interface1.

              Manoj Never Gives up

              A 1 Reply Last reply
              0
              • M Manoj Kumar Rai

                See the function name in the class A is as Interface1.ReturnString1(), and thats why you can not a.ReturnString1(). But the function name for the Interface1 is "ReturnString1" and you can call it using object of type Interface1.

                Manoj Never Gives up

                A Offline
                A Offline
                amitcoder83
                wrote on last edited by
                #7

                can u give me a code snippet about how actually u implement it

                M 1 Reply Last reply
                0
                • A amitcoder83

                  can u give me a code snippet about how actually u implement it

                  M Offline
                  M Offline
                  Manoj Kumar Rai
                  wrote on last edited by
                  #8

                  I wrote you code in the previous replies it self. In your case: A aa = new A(); //Cast it to interface Interface1 interface1 = aa; //Now u can call the Function ReturnString1 interface1.ReturnString1(); Also you can write it as: ((Interface1)aa).ReturnString1();

                  Manoj Never Gives up

                  A 1 Reply Last reply
                  0
                  • M Manoj Kumar Rai

                    I wrote you code in the previous replies it self. In your case: A aa = new A(); //Cast it to interface Interface1 interface1 = aa; //Now u can call the Function ReturnString1 interface1.ReturnString1(); Also you can write it as: ((Interface1)aa).ReturnString1();

                    Manoj Never Gives up

                    A Offline
                    A Offline
                    amitcoder83
                    wrote on last edited by
                    #9

                    thank u very much for ur proper consultation.

                    1 Reply Last reply
                    0
                    • M Manoj Kumar Rai

                      I think you are not correct:-)

                      Manoj Never Gives up

                      O Offline
                      O Offline
                      originSH
                      wrote on last edited by
                      #10

                      Manoj Kumar Rai wrote:

                      I think you are not correct

                      You could atleast provide an explination rather than going round insulting people. If you explain why then people will be thankful of learning something new rather than thinking your an arse ;)

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        amitcoder83 wrote:

                        string ReturnString1();

                        the default access for any method or property is private. If you remove 'public' then they default to private right through your code.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "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 )

                        O Offline
                        O Offline
                        originSH
                        wrote on last edited by
                        #11

                        The default access for a method is public ... "but" ... when explicitly implimenting an interface method it is automatically public and you cannot put any modifier on it, even the public one. Explicit Interface Implementation Tutorial[^] How to: Explicitly Implement Interface Members (C# Programming Guide)[^]

                        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