Solved

Regarding delegates...

Posted on 2003-12-03
8
335 Views
Last Modified: 2010-04-16
Hello experts !!!

I would like to declare fields , methods and properties
within a delegate.

The delegate should look & act like a class.

How can i achieve this ?
0
Comment
Question by:hemanthsharma
  • 5
8 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 9872496

The short answer...

use Interfaces, not delegates.

Interfaces and Delegates are essentially the same thing.

[from MSDN]

Delegates vs. Interfaces

Delegates and interfaces are similar in that they enable the separation of specification and implementation. Multiple independent authors can produce implementations that are compatible with an interface specification. Similarly, a delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification. When should you use interfaces, and when should you use delegates?

Delegates are useful when:

A single method is being called.
A class may want to have multiple implementations of the method specification.
It is desirable to allow using a static method to implement the specification.
An event-like design pattern is desired (for more information, see the Events Tutorial).
The caller has no need to know or obtain the object that the method is defined on.
The provider of the implementation wants to "hand out" the implementation of the specification to only a few select components.
Easy composition is desired.


Interfaces are useful when:

The specification defines a set of related methods that will be called.
A class typically implements the specification only once.
The caller of the interface wants to cast to or from the interface type to obtain other interfaces or classes.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9872521


If you are still hell-bent on using a delegate, I don't think you can get the exact functionality that you are looking for.

A Delegate is merely a pointer to an already existing class structure (almost always a method or function).


public delegate void myDelegate();


public class myClass
{
    myDelegate del1;
 
    public myClass()
    {
       del1 += method1();
       del1 += method2();

       del1();  // executes method1(), then executes method2()
    }
 
    private void method1() { //stuff }

    private void method2() { //stuff }
   

}
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9872534


On the other hand an interface acts a lot more like a class.


public interface ITest
{
 
   void method1();

   string function1(string);

}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 22

Expert Comment

by:_TAD_
ID: 9872543


As for specifying fields.... you cannot do that with either delegates or interfaces...



What exactly are you trying to accomplish?  
0
 
LVL 1

Author Comment

by:hemanthsharma
ID: 9873028
A delegate name and the constructor name must be the same.


So, in such a situation, how can we add variables & properties to tat particular delegate class?

I have increased the points ..!>!>! >!>
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 175 total points
ID: 9874223


I guess what I am telling you is that you CAN NOT.  I don't think you fully grasp what a deleagte really is.

A delagate only POINTS to a method or function (it is like a function call)!!

A constructor is treated just like a function!



What exactly are you trying to do?   Describe what you are doing without using the word "Delegate" ( I don't think you quite understand the concepts of delagates).
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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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