Re: [Mono-dev] Re: Constructor implementation obligation via interface?
Yeah, my replies were incorrect, I thought I'd read somewhere that was the way to do it... sadly not. Sorry about that. On 5/24/06, Ympostor [EMAIL PROTECTED] wrote: Martin Hinks escribió: You can't force a constructor via an interface... Write an abstract class with the constructor definition that you want, specify that the interface inherits the abstract class It seems an interface cannot inherit from a class. Example: public abstract class ExampleWithEmptyConstructor { public ExampleWithEmptyConstructor() { } } public interface IExample : ExampleWithEmptyConstructor { } This returns a compilation error: Error 1 Type 'ExampleWithEmptyConstructor' in interface list is not an interface. Regards -- ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list -- Martin Hinks http://www.m-s-d.net ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
Re: [Mono-dev] Re: Constructor implementation obligation via interface?
Of course strictly speaking you cannot create class WITHOUT ANY constructor. If you don't specify any, the default empty ctor is generated by compiler. You can create a class without an empty ctor though, by defining the one with some parameters. I guess the only way to statically enforce having the empty ctor in a class is by hack provided by Jb. 2006/5/24, Ympostor [EMAIL PROTECTED]: Kamil Skalski escribió: There is a slight problem. In C# empty constructors are added automatically, so you can't define a class without empty constructor. What you can do is to define a class with private empty constructor, which will prevent user from instanciating it directly. I guess there is not way to forbid this. Thanks for your comment. But sorry because I think I haven't understood you completely. You say I can't define a class without empty constructor? That's not true. When you create an empty Console application in Visual Studio, there is a class Program that contains an static method but does not have any constructor. If you were trying to say that all classes, at the compiler level, contain an empty constructor, then ok; but what I want to do is to force a class to have a public empty constructor, and, if it doesn't have it, have the compiler to warn me because of the semantic requisite. Is there a way to achieve this? Regards. -- ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list -- Kamil Skalski http://nazgul.omega.pl ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
Re: [Mono-dev] Re: Constructor implementation obligation via interface?
System.Text.Encoding.GetEncoding(...) is an example of a very complex factory built in. Here is a method i use: (this might not compile, i didn't test it, just an example) public enum ShapeType { Circle, Square, Triangle } public abstract class Shape { public static Shape GetShape(ShapeType st) { switch (st) { case ShapeType.Circle: return new Circle(); case ShapeType.Square: return new Square(); case ShapeType.Triangle(): return new Triangle(); default: throw new Exception(Unknown shape); } } public abstract int Sides { get{return -1;) } } public class Circle : Shape { public override int Sides { get{return Int32.MaxValue;} } } public class Square : Shape { public override int Sides { get{return 4;} } } public class Triangle : Shape { public override int Sides { get{return 3;} } } in the code you just say: Shape myShape = Shape.GetShape(ShapeType.Circle); Console.WriteLine(This shape has {0} sides, myShape.Sides); On Wed, 2006-05-24 at 16:35 +0200, Robert Jordan wrote: Ympostor wrote: Kamil Skalski escribió: There is a slight problem. In C# empty constructors are added automatically, so you can't define a class without empty constructor. What you can do is to define a class with private empty constructor, which will prevent user from instanciating it directly. I guess there is not way to forbid this. Thanks for your comment. But sorry because I think I haven't understood you completely. You say I can't define a class without empty constructor? That's not true. When you create an empty Console application in Visual Studio, there is a class Program that contains an static method but does not have any constructor. It has an implicit default ctor. If you were trying to say that all classes, at the compiler level, contain an empty constructor, then ok; but what I want to do is to force a class to have a public empty constructor, and, if it doesn't have it, have the compiler to warn me because of the semantic requisite. Is there a way to achieve this? No, not directly. If you really feel like you'd need this semantic sugar, you may provide a creator for the class: interface ICreator { Foo CreateFoo (); } class Foo { public Foo () { } } class FooCreator : ICreator { public Foo CreateFoo() { return new Foo (); } } Search the Web for C# factory pattern. Robert ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list -- Zac Bowling [EMAIL PROTECTED] ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list