Reputation: 1
I have a number of classes all inherited from the same base class (clsComponent). I store these in
public Dictionary<string, clsComponent> Components = new Dictionary<string, clsComponent>();
I add objects of inherited classes to this dictionary. One example (several other classes but just an example)
Components.Add("COMP", new clsCompressor(this,Settings, new RectangleF(50,50,50,50)) );
So now I expect to cycle through the classes in Components, redefine their position and paint them
public void PaintSystem(Graphics grph)
{
grph.Clear(Color.White);
Region reg = grph.Clip;
RectangleF rectot = reg.GetBounds(grph);
foreach (KeyValuePair<string, clsComponent> entry in Components)
{
entry.Value.UpdateLocation(rectot);
entry.Value.Paint(grph);
}
UpdateLocation is a sub in the base class so that works fine. Paint is a sub in the base class, but hidden by another Paint in each inherited class. So when I do this UpdateLocation works fine, but Paint is calling the sub in the base class, not in the derived that was entered in the Dictionary.
Paint sub in base class :
public void Paint(Graphics grph) { }
Paint sub in its inherited class:
public class clsCompressor : clsComponent
{
public clsCompressor(clsRefrigeration Syst, clsSettings Set, RectangleF PctLoc)
{
RefSystem = Syst;
Settings = Set;
PctLocation = PctLoc;
}
clsRefrigeration RefSystem = null;
clsSettings Settings = null;
new public void Paint(Graphics grph)
{
// do the real painting here
}
}
When cycling in the above foreach the type of entry.Value is the correct class, not the base class, so why does it call the wrong function ?
Upvotes: 0
Views: 21