473,763 Members | 9,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2955
<hu*******@yaho o.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.co m>
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*******@yaho o.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*******@yaho o.comwrote:
>>>I have an interface for which I want to define a particular
constructo r. I want to force the user to implement this particular
constructo r. 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 "identifyin g 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
1517
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 http://www.cs.kuleuven.ac.be/~ares/tmp/array_2_hpp.html It is a 2d array that dynamically allocates its storage. It cannot be resized. It offers element access methods for both sequential access and 2d element access.
7
17194
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
4589
by: Duncan McNutt .[FTSE] | last post by:
Why does code derive from for example, IComparer ie, class SomeClass : IComparer { public SomeClass() { }
12
12469
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 contracts, but why wouldn't you be allowed to have internal contracts also, not necessarily exposing the members publicly? You're not allowed to cast an object to the internal interface, but I'm unable to keep the members internal.
17
2354
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
8
1577
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 design pattern and other classes are exposed to the outside world, but the majority are internal classes and therefore IMO they don't require an interface. I can find plenty of articles on how to implement interfaces but I can't find any...
4
1621
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 dont need ways/design patterns around this issue. What I want to know is why the CLR does not allow this. Regards,
42
2701
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. Well, I came across Scott Meyer's SmartPtr example from some 10 years ago. I like the template member function for type conversion to solve inheritance issues. On MSVS 2003, I get the below error, if I declare the constructor SmartPtr(T*...
2
1683
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 judiciously checked almost all constructs with both compiler before coding, but during a heavy session, I forgot to check the user conversion part, and now, I am stuck big time :( So here is my user conversion for a smart pointer class (direct...
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.