bklare

My question is, how may an object be created as a base class object be changed to a derived object in the base contructor

For example, I have objects may be created in few different ways, one being an XmlElement that has the necesarry information. The element has an attribute which determines what derived class the object is going to be. So instead of everytime I want to create an object having to do a switch statement to determine what type it is, I want to put this logic in the base contructor making the code more elegant and modular.

Can this be done



Re: Visual C# Language Morph in Constructor

OmegaMan

The short answer is no. If one is creating a base object, it is not a derived object by any stretch. You describe a factory pattern where an object is created by specification. Your post describes such a factory being in the base classe's constructor, which defeats the purpose of having derived classes, if the base knows about all deriveds...





Re: Visual C# Language Morph in Constructor

bklare

Thank you for your response. I will use a programatic solution for this by having a simple function take the constructor parameters and return the determined derived object, thus reducing a repeat of code through the program.





Re: Visual C# Language Morph in Constructor

OmegaMan

A couple of thoughts on elegance so to speak, have the base class call a virtual method on itself called Setup which will do a majority of the initial setup activities (or possibly none). Have the derived class optionally override the method and have it setup its items and possibly call the base classes's Setup to finish off it. That way each of the objects are created in a consistent manner.

Also have the base class adhere to an interface, that way if you do have a factory object, one can create the actual derived object in the factory but have it return the interface, hence achieving functionality without having to work with the actual derived class(es).





Re: Visual C# Language Morph in Constructor

bklare

I agree, those are strong points. My objects already use these important OO practices. Thanks for your help.





Re: Visual C# Language Morph in Constructor

Matthew Watson


...have the base class call a virtual method on itself called Setup which will do a majority of the initial setup activities (or possibly none). Have the derived class optionally override the method and have it setup its items and possibly call the base classes's Setup to finish off it.


Do you mean you would call Setup() from the base class's constructor, like this



public class Base
{
Base()
{
Setup();
}

public virtual void Setup()
{
// Do nothing.
}
}



You have to be careful with that kind of setup, and indeed CodeAnalysis will throw up a warning about calling virtual methods from a constructor.
See http://msdn2.microsoft.com/en-us/library/ms182331(VS.80).aspx

In the following code:



public class Derived: Base
{
private string importantThingThatMustBeInitialised = "Not Initialised";

public Derived()
{
importantThingThatMustBeInitialised = "Initialised";
}

public override void Setup()
{
MessageBox.Show(importantThingThatMustBeInitialised);
}
}

...

Derived d = new Derived();



When a Derived object is created, its "Setup()" method will be called BEFORE its constructor!
Clearly, this could cause a bit of confusion...