Home All Groups Group Topic Archive Search About

Interfaces & Constructors

Author
7 Sep 2006 5:47 AM
hufaunder
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

Author
7 Sep 2006 6:02 AM
Jon Skeet [C# MVP]
<hufaun***@yahoo.com> 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.

--
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
Author
7 Sep 2006 9:41 AM
Wiebe Tijsma
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:
Show quote
>  <hufaun***@yahoo.com> 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.
>
Author
7 Sep 2006 1:16 PM
Gaurav Vaish (www.EduJiniOnline.com)
> 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:
>>  <hufaun***@yahoo.com> 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

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
-------------------
Author
7 Sep 2006 3:15 PM
Ben Voigt
> 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.
Author
11 Sep 2006 2:09 AM
Gaurav Vaish (www.EduJiniOnline.com)
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
-------------------
Author
7 Sep 2006 2:50 PM
hufaunder
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:
Show quote
> 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.
> >
Author
7 Sep 2006 9:32 AM
Marcin_Grzêbski
Hi,

hufaun***@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

AddThis Social Bookmark Button