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. Define interface / abstract class with constructor

Define interface / abstract class with constructor

Scheduled Pinned Locked Moved C#
questionhelp
5 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.
  • F Offline
    F Offline
    Florian Storck
    wrote on last edited by
    #1

    Hello, I'm trying to to the following thing: public abstract class ITest { public abstract ITest(string val); // causes error CS0106: The modifier 'abstract' is not valid for this item } public class Test : ITest { public Test(string val) // this constructor MUST be implemented { // by any inheriting classes // do something } } Is there any possibility for defining abstract constructors ? Interfaces can't define constructors. It's not important because I realized that it is not necessary, but nevertheless it's an interesting question. thanks, Florian

    N C 2 Replies Last reply
    0
    • F Florian Storck

      Hello, I'm trying to to the following thing: public abstract class ITest { public abstract ITest(string val); // causes error CS0106: The modifier 'abstract' is not valid for this item } public class Test : ITest { public Test(string val) // this constructor MUST be implemented { // by any inheriting classes // do something } } Is there any possibility for defining abstract constructors ? Interfaces can't define constructors. It's not important because I realized that it is not necessary, but nevertheless it's an interesting question. thanks, Florian

      N Offline
      N Offline
      NrmMyth
      wrote on last edited by
      #2

      Abstract classes can't be initialized, and constructors can't be virtual => no need for constructors. Abstract is a class-keyword...

      1 Reply Last reply
      0
      • F Florian Storck

        Hello, I'm trying to to the following thing: public abstract class ITest { public abstract ITest(string val); // causes error CS0106: The modifier 'abstract' is not valid for this item } public class Test : ITest { public Test(string val) // this constructor MUST be implemented { // by any inheriting classes // do something } } Is there any possibility for defining abstract constructors ? Interfaces can't define constructors. It's not important because I realized that it is not necessary, but nevertheless it's an interesting question. thanks, Florian

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        You have an abstract CLASS called ITest, this is confusing. The convention is that interface names start with I[interface name], not class names. If you do need to do some common work at construction time in the abstract class then use

        protected ITest(string val)
        {
        // Do common stuff relevant to the abstract class
        // This can be empty if you want to force derived classes
        // to implement it.
        }

        Florian Storck wrote:

        Is there any possibility for defining abstract constructors ?

        Yes, see above.


        Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        F 1 Reply Last reply
        0
        • C Colin Angus Mackay

          You have an abstract CLASS called ITest, this is confusing. The convention is that interface names start with I[interface name], not class names. If you do need to do some common work at construction time in the abstract class then use

          protected ITest(string val)
          {
          // Do common stuff relevant to the abstract class
          // This can be empty if you want to force derived classes
          // to implement it.
          }

          Florian Storck wrote:

          Is there any possibility for defining abstract constructors ?

          Yes, see above.


          Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          F Offline
          F Offline
          Florian Storck
          wrote on last edited by
          #4

          Hi Colin, thanks for your answer. By the way: do you have a certain style for naming abstract classes (probably ATest, I guess ;-) ). I didn't changed the name because I'm used to C++ were abstract classes and interfaces are handled the same way (I do C# programming for basically 5 months now) ... so this difference in naming makes only really sense (for me), if the abstract class implements things, so it's not completely abstract anymore... but ok, C# has the interface type/definition, then it makes probably more sense. Bye, Florian

          C 1 Reply Last reply
          0
          • F Florian Storck

            Hi Colin, thanks for your answer. By the way: do you have a certain style for naming abstract classes (probably ATest, I guess ;-) ). I didn't changed the name because I'm used to C++ were abstract classes and interfaces are handled the same way (I do C# programming for basically 5 months now) ... so this difference in naming makes only really sense (for me), if the abstract class implements things, so it's not completely abstract anymore... but ok, C# has the interface type/definition, then it makes probably more sense. Bye, Florian

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Florian Storck wrote:

            do you have a certain style for naming abstract classes (probably ATest, I guess ).

            No, there is very little hungarian notation used in .NET. It is generally frowned upon. I for interface is one of the very few examples.


            Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

            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