?
Solved

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

Posted on 2003-03-26
3
Medium Priority
?
484 Views
Last Modified: 2008-02-01
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();
}
 
0
Comment
Question by:pedery
1 Comment
 

Accepted Solution

by:
shankarbiyer earned 84 total points
ID: 8223097
hi,
Please go through the changes i have made to your code below.I think now it works as you had desired.

If its not as you had desired please let me know.I can try another approach


using System;
using System.Windows.Forms;  

namespace RDProj
{
     /// <summary>
     /// Summary description for Delegate.
     /// </summary>
     public class MainClass
     {
          public SuperClass SC;
          public SuperClass SC1;
         
          public MainClass()
          {
               SC = new SubClass();
               SC1=new SuperClass();

               SC.rotateCW();   // MessageBox with "CW/CCW called", correct!
               
               //Now this works as u desired.Call is made to that function of Subclass
               // which we have registered with the delegate
               SC.rotateCCW1();  // MessageBox with "CW/CCW called", correct!
               
               
               //This call will call the method in base class as in base class we have registered the delegate
               //with base class method
               SC1.rotateCCW1();//MessageBox with "CCW called", correct!
          }

          public class SuperClass
          {
               public delegate void rotateCCWDelegate();
               public rotateCCWDelegate rotateCCW1;
               public SuperClass() {
               rotateCCW1 = new rotateCCWDelegate(rotateCW);
               }

               public virtual void rotateCW() {MessageBox.Show("CW");}
               public virtual void rotateCCW() {MessageBox.Show("CCW");}
          } // End superclass

          public class SubClass : SuperClass
          {
               //Declare this in Superclass as if u declare it there the super class
               //isnt aware of this delegate .
               //public rotateCCWDelegate rotateCCW1;
               
               public SubClass()
               {
                    rotateCCW1 = new rotateCCWDelegate(rotateCW);
               }

               public override void rotateCW()  
               {
                    MessageBox.Show("CW/CCW called");
               }

               //public delegate void rotateCCWDelegate();
          }
     }
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question