I want to create an abstract base class for all paramter-type classes to inherit from in my application. All paramters will have name, ID and required properties.
All parameters will have their properties set from XML via the SetProperties method (the XmlParser class shown below is just for demonstration).
Since all parameters will have the same 3 properties, I wanted the base class to set those properties but have the inheriting class extend the SetProperties method to set the additional properties it contains.
I was thinking of something like overriding events on a Control.
Here is a sample of what I was thinking, although it does not work.
abstract class ParameterBase
{
protected string ParameterName;
protected bool IsRequired;
protected int ParameterId;
public abstract void SetProperties(string xml)
{
this.ParameterName = XmlParser.GetParameterName(xml);
this.IsRequired = XmlParser.GetIsRequired(xml);
this.ParameterId = XmlParser.GetParameterId(xml);
}
}
abstract class Parameter1 : ParameterBase
{
private string _value;
public string ParameterName
{
get { return base.ParameterName; }
}
public bool IsRequired
{
get { return base.IsRequired; }
}
public int ParameterId
{
get { return base.ParameterId; }
}
public string Value
{
get { return _value; }
}
public Parameter1()
{
}
public override void SetProperties(string xml)
{
base.SetProperties(xml);
_value = XmlParser.GetValue(xml);
}
}
I would do simply, like this:
and derived one:
It’s easy and clearer to manage in this way. Move common properties in separate base class it’s good, but persistance management (Save/Load), leave to children. They should know how to do that.
Code provided has a couple of problems:
abstractmethod can not have bodyyou have strange
public override void SetValues(string xml)which I think should bepublic override void SetProperties(string xml)