tag:blogger.com,1999:blog-1688176320920284646.post1110699219131143196..comments2023-12-20T10:24:23.540+00:00Comments on A day in the life of...: Calling base constructors in C#Unknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-1688176320920284646.post-60645336798178220812012-01-12T03:10:06.213+00:002012-01-12T03:10:06.213+00:00I'm a little late to the conversation, but see...I'm a little late to the conversation, but seeing as my search lead me here, it may be worth updating.<br /><br />This has always bothered me, too, but I usually find a way to make it work using abstract or virtual methods declared in the base class. <br /><br />It's not nearly as flexible as being able to call a constructor at will, but Anders has a very valid point.<br /><br />As an object consumer, I need to know that an object will provide a minimum level of service. The moment derived code is allowed to run before the constructor, I can throw my expectations out the window. The derived object could pass itself to me half-initialized, and I wouldn't be prepared for it, because object X is <i>always</i> supposed to be fully initialized. That's really just the tip of the iceberg, but the rest are mostly different flavors of the above.<br /><br />As it stands, customization can be achieved using calls to an abstract member; however, their introduction into the base class ensures its consumers will be aware of it. It would still be possible to say, "If you account for this, this, and this, then these are your minimum expectations," and that confidence is what makes OO possible without a constant Aspirin IV drip.Anonymous Developerhttps://www.blogger.com/profile/05380592608437122004noreply@blogger.comtag:blogger.com,1999:blog-1688176320920284646.post-86848514683490161472008-12-10T16:01:00.000+00:002008-12-10T16:01:00.000+00:00I had the same problem and sometimes is annoying n...I had the same problem and sometimes is annoying not to have the hability to call the base constructor. <BR/><BR/>My solution is avoiding constructors :) and use static methods (that a call creators) or public methods that must always call after the "new" (that a call initiators). Of course, this is not the best solution.<BR/><BR/>I exacly don't see Anders point, but when they have arguments about the safety or because it breaks OO paradigm I fell uneasy. The designers of C# should realy have more confident on what programers can do ;)Mariohttps://www.blogger.com/profile/05990928890800438792noreply@blogger.comtag:blogger.com,1999:blog-1688176320920284646.post-70830851501604544332008-06-25T14:42:00.000+01:002008-06-25T14:42:00.000+01:00Richard, but when that method is virtual you run i...Richard, but when that method is virtual you run into trouble...<BR/><BR/>public class BaseObject<BR/>{<BR/> public BaseObject()<BR/> {<BR/> DoSomething();<BR/> }<BR/> protected void DoSomething()<BR/> {<BR/> }<BR/>} <BR/><BR/>public class NewObject : BaseObject<BR/>{<BR/> private Person Owner;<BR/> public NewObject() : base()<BR/> { <BR/> Owner = new Person();<BR/> }<BR/><BR/> protected override DoSomething()<BR/> {<BR/> Owner.DoSomething();<BR/> }<BR/>}<BR/><BR/>What would be better is this...<BR/><BR/> public NewObject()<BR/> { <BR/> Owner = new Person();<BR/> base();<BR/> }<BR/><BR/>.NET allows it, just not C#!Peter Leslie Morrishttps://www.blogger.com/profile/09377536995235480828noreply@blogger.comtag:blogger.com,1999:blog-1688176320920284646.post-16131969422717605702008-06-20T13:57:00.000+01:002008-06-20T13:57:00.000+01:00What do you expect of a Dane?! (only kidding, Ande...What do you expect of a Dane?! (only kidding, Anders is a legend)<BR/><BR/>I was having the same problem. Read your post, scratched my head and came up with this:<BR/><BR/>class Dane<BR/>{<BR/> public Dane(string fullName)<BR/> {<BR/> this.FullName = fullName;<BR/> }<BR/> string FullName = "";<BR/>}<BR/><BR/>class Anders<BR/>{<BR/> public Anders(string fName, string lName) : base(ConcatName(fName, lName))<BR/> {<BR/> //blue blah<BR/> }<BR/><BR/> public static string ConcatName(string fName, string lName)<BR/> {<BR/> return "The Great Dane " + fName + lName; <BR/> }<BR/>}<BR/><BR/>Ofcourse the method called has to be static which may introduce futher complication, depending on your needs.<BR/><BR/>Hope it helps<BR/><BR/>Richard (hlanvill at gmail dot com)Unknownhttps://www.blogger.com/profile/13721188276685858413noreply@blogger.comtag:blogger.com,1999:blog-1688176320920284646.post-88326605908522519112007-01-19T13:44:00.000+00:002007-01-19T13:44:00.000+00:00The problem is that he is explaining how to solve ...The problem is that he is explaining how to solve a problem that is different to what I asked.<br /><br />I didn't ask for named constructors, I asked for the ability to call a base constructor from any point within my constructor's code rather than always at the start.Peter Leslie Morrishttps://www.blogger.com/profile/09377536995235480828noreply@blogger.com