Is there a way to automaticly call all versions of an inherited method?
Posted
by Eric
on Stack Overflow
See other posts from Stack Overflow
or by Eric
Published on 2009-12-15T23:38:32Z
Indexed on
2010/06/18
2:03 UTC
Read the original article
Hit count: 294
I'm writing a plug-in for a 3D modeling program. I have a custom class that wraps instances of elements in the 3D model, and in turn derives it's properties from the element it wraps. When the element in the model changes I want my class(es) to update their properties based on the new geometry.
In the simplified example below. I have classes AbsCurveBasd, Extrusion, and Shell which are all derived from one another. Each of these classes implement a RefreshFromBaseShape() method which updates specific properties based on the current baseShape the class is wrapping.
I can call base.RefreshFromBaseShape() in each implementation of RefreshFromBaseShape() to ensure that all the properties are updated. But I'm wondering if there is a better way where I don't have to remember to do this in every implementation of RefershFromBaseShape()? For example because AbsCurveBased does not have a parameterless constructor the code wont even compile unless the constructors call the base class constructors.
public abstract class AbsCurveBased
{
internal Curve baseShape;
double Area{get;set;}
public AbsCurveBased(Curve baseShape)
{
this.baseShape = baseShape;
RefreshFromBaseShape();
}
public virtual void RefreshFromBaseShape()
{
//sets the Area property from the baseShape
}
}
public class Extrusion : AbsCurveBased
{
double Volume{get;set;}
double Height{get;set;}
public Extrusion(Curve baseShape):base(baseShape)
{
this.baseShape = baseShape;
RefreshFromBaseShape();
}
public override void RefreshFromBaseShape()
{
base.RefreshFromBaseShape();
//sets the Volume property based on the area and the height
}
}
public class Shell : Extrusion
{
double ShellVolume{get;set;}
double ShellThickness{get;set;}
public Shell(Curve baseShape): base(baseShape)
{
this.baseShape = baseShape;
RefreshFromBaseShape();
}
public void RefreshFromBaseShape()
{
base.RefreshFromBaseShape();
//sets this Shell Volume from the Extrusion properties and ShellThickness property
}
}
© Stack Overflow or respective owner