Solved

Regarding delegates...

Posted on 2003-12-03
8
334 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

786 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