Re: calling base constructor

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Sat, 01 Mar 2008 01:43:55 -0800, Peter Morris <peter[dot]morris <"capableobjects.com>"> wrote:

I once wrote to Anders H and asked why you can't call the base constructor
at any point within your body. He was kind enough to reply with a detailed
answer, unfortunately it wasn't an answer to the question I asked :-) He
answered why C# doesn't have named constructors and not why you can't decide
where to call a base constructor.

I can't speak for him or others involved in the design of C#. However, to me it makes perfect sense that you can't call the base constructor except at the very beginning of the derived constructor. A classic convention in OOP is that by the time the derived class executes any code, the base class has been completely constructed. Allowing the base constructor to be called elsewhere would break this fundamental approach.

As far as your example goes:

I have found this a bit of a pain when the base constructor calls another
method, for example.

public class BaseClass
{
protected List<Member> Members;
public BaseClass()
{
Members = new List<Member>();
InitializeDefaultValues();
}

protected virtual void InitializeMembers()
{
foreach(Member currentMember in Members)
currentMember.Initialize();
}
}

public class ChildClass : BaseClass
{
public ChildClass() : base()
{
Members.Add(new Member("Hello");
Members.Add(new Member("Goodbye");
}
}

In this case Hello and Goodbye wont get initialized, I'd have to either
explicitly call InitializeMembers or duplicate the initialization code..

Here, the base class clearly expects the derived class to override the initialization behavior. In that case, it seems obvious to me that the addition of new elements to the Members field belongs in an override of the InitializeMembers() method, given the existing design.

You don't have to duplicate anything. You just have to override the virtual method, put your initialization in there, and then call the base method after the initialization happens (yes, it's an explicit call to InitializeMembers(), but it's from the override, which is a perfectly normal and appropriate thing to do).

Frankly, the fact that in your example the InitializeMembers() method is virtual makes a very strong suggestion that it doesn't belong in the constructor at all, and that the class really should have a slightly different initialization model. One in which that initialization is handled separately from construction of the object. But even given the exact design above, the right way to deal with initialization is obvious and completely workable.

It would be nice if this changed in my opinion, but I don't suppose it will.

I hope it doesn't. There are always good ways around the apparent limitation, and IMHO the limitation makes for a robust, reliable language. There's a reason that the limitation is present in many OOP languages (all of the ones I know of, in fact, including C#, Java, and C++).

Pete
.



Relevant Pages

  • Re: getting used to Java - question about "style"
    ... > "Subclasses may override nonfinal methods and Java will dispatch a call to ... > before executing the derived class constructors. ... > constructor." ... public class B extends A ...
    (comp.lang.java.programmer)
  • Re: Message Builder vs. a Build Method?
    ... Builder class would encapsulate all the complex algorithms for making ... OTOH, as Daniel T. suggests, sometimes the initialization requires unique processing for initialization that is clearly intrinsic to the object itself. ... Constructors tend to be fragile and it is difficult to manage errors when they occur in a constructor scope, so it is usually a good idea to keep the processing in constructors as simple as possible. ... When the initialization of attribute data requires complex processing AND it seems like is intrinsic ot the object, that justifies having a separate initialization method that is invoked immediately after the constructor. ...
    (comp.object)
  • Re: Localization Service
    ... these assets are contents to be edited by the users. ... problem with a validatorFactory. ... I was filling the dictionary on the constructor. ... beware of too much deferred initialization. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Adventures in C# Unit Testing
    ... public class ChildClass:ParentClass ... private IMyInterface mockMyObject; ... ChildClass overrides createMyObject to return a mock object created by the ChildClass constructor. ... Subclass and override. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Constructor as a "Reset" Button
    ... constructor is more or less just for initialization, ... In PHP you might get ... And HIPPA certification is not easy - nor is it cheap. ...
    (comp.lang.php)