473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Interfaces & Constructors

I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?

Thanks

Sep 7 '06 #1
7 2925
<hu*******@yahoo.comwrote:
I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?
You can't specify this, I'm afraid. .NET itself doesn't have support
for it.

With generic constraints you can enforce that the type being used has a
parameterless constructor, but that's all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #2
Hi,

hu*******@yahoo.com napisał(a):
I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?

Thanks
As Michael mentioned before, you have to consider the
abstract class.
An interface is a contract which works on a class instances,
after they are initialized (not before)!

With regards
Marcin
Sep 7 '06 #3
Additionally, wanting something like this usually indicates a flaw in
the object design, see if you can solve your problem with:
- a factory pattern
- (abstract) base class (maybe in combination with a (simple) interface)
Jon Skeet [C# MVP] wrote:
<hu*******@yahoo.comwrote:
>>I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?


You can't specify this, I'm afraid. .NET itself doesn't have support
for it.

With generic constraints you can enforce that the type being used has a
parameterless constructor, but that's all.
Sep 7 '06 #4
Additionally, wanting something like this usually indicates a flaw in the
object design, see if you can solve your problem with:
- a factory pattern
- (abstract) base class (maybe in combination with a (simple) interface)
Jon Skeet [C# MVP] wrote:
> <hu*******@yahoo.comwrote:
>>>I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
Abstract class without a default construct would definitely be the simplest
and easiest way to do it.
At the same time, you may want to have a look at ObjectBuilder pattern --
part of Smart Client Software Factory.
SCSF definitely will need a lot of study before you can go ahead to start
using them. In simplest bird's-eye view for using SCSF, you will need to
implement a "strategy" of "identifying the constructors" for instantiation.

But then, SCSF may or may not be suitable... will depend on the "bigger
picture" of your application.
--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://articles.edujinionline.com/webservices
-------------------

Sep 7 '06 #5
An abstract class is what I should use. I think that will work just
fine for my scenario. Thanks for the tip.

Wiebe Tijsma wrote:
Additionally, wanting something like this usually indicates a flaw in
the object design, see if you can solve your problem with:
- a factory pattern
- (abstract) base class (maybe in combination with a (simple) interface)
Jon Skeet [C# MVP] wrote:
>I have an interface for which I want to define a particular
constructor. I want to force the user to implement this particular
constructor. Even better would be if I could make sure there is no
default constructor. That might be a bit messy because the implementer
of the interface should have the freedom to add any
function/constructor he wants on top of the interface. But can't I at
least force that one particular constructor must be present? I tried it
but got the following error "Interfaces cannot contain constructors".
Therefore, I wonder if it is impossible to do what I want to do. If so
where does this limitation come from?

You can't specify this, I'm afraid. .NET itself doesn't have support
for it.

With generic constraints you can enforce that the type being used has a
parameterless constructor, but that's all.
Sep 7 '06 #6
Abstract class without a default construct would definitely be the
simplest and easiest way to do it.
This can't prevent the derived class from providing a default constructor,
nor prevents the derived class from requiring additional parameters.

abstract class A
{
public A(bool needed) {}
}

class B : A
{
public B() : base(false) {} // see -- still have a default
constructor
}

There is no way to force or forbid a derived class to provide a constructor
with a particular signature. You'd need reflection to call it dynamically
anyway -- use the Factory pattern instead.
Sep 7 '06 #7
Points taken and duly accepted.
This can't prevent the derived class from providing a default constructor,
nor prevents the derived class from requiring additional parameters.
However, the base (abstract) class is always provided with a parameter
to its constructor(s).
There is no way to force or forbid a derived class to provide a
constructor with a particular signature. You'd need reflection to call it
dynamically anyway -- use the Factory pattern instead.
Precisely... where one may wish to use ObjectBuilder pattern.
--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://articles.edujinionline.com/webservices
-------------------
Sep 11 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ares Lagae | last post by:
Hello, I am trying to create a container the stl way, and I have a couple of questions. The code of the container in question can be found at ...
7
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
19
by: Duncan McNutt .[FTSE] | last post by:
Why does code derive from for example, IComparer ie, class SomeClass : IComparer { public SomeClass() { }
12
by: Anders Borum | last post by:
Hello! I was wondering why we're allowed to define interfaces as internal, when all the members of the interface, when implemented in a class, are made public? I know that interfaces serve as...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
8
by: Dave | last post by:
I have a set of developers who have gone off and implemented an interface for nearly all classes in a project\solution, now some of these classes will need interfaces as they implement the provider...
4
by: Adam | last post by:
Okay, so I know this will come off as a stupid question...but I am going to ask it anyways... I know you are not allowed to have constructors defined in an interface, but why not? I really...
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
2
by: coder_lol | last post by:
MS VS 7.0 happily resolves by SmartPointer and Inheritance, but I got to use another target compiler and it does not accept user conversion for templates. Can I forced a recast somehow? I have...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.