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. Interface does not alter class, or does it? [modified]

Interface does not alter class, or does it? [modified]

Scheduled Pinned Locked Moved C#
questionhelp
8 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
    mizitras
    wrote on last edited by
    #1

    This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007

    P D 3 Replies Last reply
    0
    • M mizitras

      This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Unless something was lost in translation, and certainly some code was, the problem is that 'Punta.Search()' is inaccessible due to its protection level which can't be fixed without changing class Punta. An interface won't fix the situation as posted.

      U 1 Reply Last reply
      0
      • P PIEBALDconsult

        Unless something was lost in translation, and certainly some code was, the problem is that 'Punta.Search()' is inaccessible due to its protection level which can't be fixed without changing class Punta. An interface won't fix the situation as posted.

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        Agree, either you add a public or if you use an interface and use it explicit you have to add the interface name plus a . to "Search"

        -^-^-^-^-^- no risk no funk ................... please vote ------>

        P 1 Reply Last reply
        0
        • U Urs Enzler

          Agree, either you add a public or if you use an interface and use it explicit you have to add the interface name plus a . to "Search"

          -^-^-^-^-^- no risk no funk ................... please vote ------>

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          ... and you'd still have to add public to the method.

          U 1 Reply Last reply
          0
          • M mizitras

            This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            "double void" ? I want one of those!

            1 Reply Last reply
            0
            • P PIEBALDconsult

              ... and you'd still have to add public to the method.

              U Offline
              U Offline
              Urs Enzler
              wrote on last edited by
              #6

              not when you explicitly implement it, then no modifier is allowed.

              -^-^-^-^-^- no risk no funk ................... please vote ------>

              1 Reply Last reply
              0
              • M mizitras

                This was a question on a students exam: Shown was a static void main, and a second class, titled Punta. namespace Test { class Test { static void Main(string[] args) { Punta punt = new Punta(); double bla = punt.Search(); } } class Punta { double Search() { return 5.0; //assume we calculated something here and found it } } } Question was: Is this code executable? (Exam) Answer: No Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why? And, how can you fix that, without changing the code of Punta-class. The expected answer for the teacher was: An interface. Followed by lines of code to make it accessible. BUT... it did say, "you may NOT change the code of the class 'Punta'". But how is that some people say: If you use an interface, you're actually modifying the class. And some say, you aren't. Which is it? -- modified at 23:04 Wednesday 5th September, 2007

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                mizitras wrote:

                Question was: Is this code executable? (Exam) Answer: No

                Correct, it won't even compile.

                mizitras wrote:

                Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why?

                You can suppress warnings, not the errors. No, you still won't be able to compile this thing since the "double void Search()" will confuse the compiler. It won't make any assumptions about what you really mean here. You cannot have two return types.

                mizitras wrote:

                And, how can you fix that, without changing the code of Punta-class.

                You can't. The Punta class MUST be changed to specify a single return type for the Search method. Also, since the default protection level is private, you must specify the Search method as public or internal for it to be seen outside the Punta class. An interface will NOT fix this problem since interfaces do not modify a class. Interfaces specify the methods that an implementing class must define. The class must still be modified to tell it which interface it's implementing. Either way, the Punta class must be modified in order for it to be fixed. -- modified at 12:04 Tuesday 4th September, 2007

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                M 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  mizitras wrote:

                  Question was: Is this code executable? (Exam) Answer: No

                  Correct, it won't even compile.

                  mizitras wrote:

                  Q for codeproject: CAN you compile, link and execute wrong code like this? Or is the only way to do so by supressing warnings and errors from the compiler? Question 2 was: If it's not executable (which was assumed you've noticed), the question was: Why?

                  You can suppress warnings, not the errors. No, you still won't be able to compile this thing since the "double void Search()" will confuse the compiler. It won't make any assumptions about what you really mean here. You cannot have two return types.

                  mizitras wrote:

                  And, how can you fix that, without changing the code of Punta-class.

                  You can't. The Punta class MUST be changed to specify a single return type for the Search method. Also, since the default protection level is private, you must specify the Search method as public or internal for it to be seen outside the Punta class. An interface will NOT fix this problem since interfaces do not modify a class. Interfaces specify the methods that an implementing class must define. The class must still be modified to tell it which interface it's implementing. Either way, the Punta class must be modified in order for it to be fixed. -- modified at 12:04 Tuesday 4th September, 2007

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  M Offline
                  M Offline
                  mizitras
                  wrote on last edited by
                  #8

                  Aah dang, I accidentally typed "double void" instead of just double and void in the Search(void) :mad: Now I've posted the wrong question :(

                  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