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