?
Solved

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

Posted on 2003-03-26
3
Medium Priority
?
479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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