Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regarding delegates...

Posted on 2003-12-03
8
Medium Priority
?
339 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
[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
  • 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
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!

 
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 700 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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