1
Answer

Using a delegate in a subclass to override the virtual method in the superclass

Administrator

Administrator

21y
1.8k
1
When using a delegate method in SubClass, the method works as it should. The same if I cast SC to SubClass. However I wish that the delegate method in SubClass should override the method in SuperClass. Unfortunately, override is not a valid keyword when creating delegate methods and the delegate does not have any methods on it's own to designate it an override property. public class MainClass { public SuperClass SC; public MainClass() { SC = new SubClass(); SC.rotateCW(); // MessageBox with "CW/CCW called", correct! SC.rotateCCW(); // MessageBox with "CCW" <--- They should point to the same method!!! } public class SuperClass { public SuperClass() {} public virtual void rotateCW() {MessageBox.Show("CW");} public virtual void rotateCCW() {MessageBox.Show("CCW");} } // End superclass public class SubClass : SuperClass { public rotateCCWDelegate rotateCCW; public SubClass() { rotateCCW = new rotateCCWDelegate(this.rotateCW); } public override void rotateCW() { MessageBox.Show("CW/CCW called"); } public delegate void rotateCCWDelegate(); }
Answers (1)