Solved

Regarding delegates...

Posted on 2003-12-03
8
332 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now