Link to home
Start Free TrialLog in
Avatar of pedery
pedery

asked on

Using a delegate in a subclass that should override the method in the superclass...

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();
}
 
ASKER CERTIFIED SOLUTION
Avatar of shankarbiyer
shankarbiyer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial