|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Interfaces & ConstructorsI 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 <hufaun***@yahoo.com> wrote:
> I have an interface for which I want to define a particular You can't specify this, I'm afraid. .NET itself doesn't have support > 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? 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 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. > > Additionally, wanting something like this usually indicates a flaw in the Abstract class without a default construct would definitely be the simplest > 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 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 ------------------- > Abstract class without a default construct would definitely be the This can't prevent the derived class from providing a default constructor, > simplest and easiest way to do it. > 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. Points taken and duly accepted.
> This can't prevent the derived class from providing a default constructor, However, the base (abstract) class is always provided with a parameter > nor prevents the derived class from requiring additional parameters. to its constructor(s). > There is no way to force or forbid a derived class to provide a Precisely... where one may wish to use ObjectBuilder pattern.> constructor with a particular signature. You'd need reflection to call it > dynamically anyway -- use the Factory pattern instead. -- Happy Hacking, Gaurav Vaish | http://www.mastergaurav.com http://articles.edujinionline.com/webservices ------------------- 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. > > Hi,
hufaun***@yahoo.com napisa³(a): > I have an interface for which I want to define a particular As Michael mentioned before, you have to consider the> 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 abstract class. An interface is a contract which works on a class instances, after they are initialized (not before)! With regards Marcin |
|||||||||||||||||||||||