473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is delegate necessary?

Hi,

A typical way of using delegates, for example, is:

class MyForm
{

void Button1.Click += new EventHandler (Action1);
void Button1.Click += new EventHandler (Action2);
void Button1.Click += new EventHandler (Action3);
}

This is equivalent to the following:

class MyForm
{

void Button1.Click()
{
Action1;
Action2;
Action3;
}
}

Therefore, delegates can be replaced by regular method calls. I don't
see the need to use delegates. Anyone could explain why use them?

Mar 23 '07 #1
1 913
If you take someone else's class and want *your* methods to file when
*their* process milestones or events (i.e. click) occur, what are you going
to do to get them to wire up your methods to their class, i.e. put your
Action1 into their Click()? .. Call them up on the phone? "Yes, hi, sorry
for bothering you, can you please fire my custom Click() event?"

Event binding via delegates allows whoever's raising the event to trigger
the methods without necessarily knowing what they are. It's actually very
powerful--think passing a reference to your method as though it were a
string or some other variable, and let these other objects fire off your
code at the right times without you having to call your method yourself.

Incidentally, this is impossible:

class MyForm
{

void Button1.Click()
{
...
}
}

You're inferring that your MyForm class will handle the Click() event on
Button1. It won't. The dot also makes for bad syntax.

But you can declare a "void Click()" method and reference that in a delegate
so that Button1 calls your Click() method.

class MyForm
{
void Form_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}

void Button1_Click(object sender, EventArgs e)
{
Action1();
Action2();
Action3();
}
}
Mar 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
11
by: Florian A. | last post by:
Hi everyone! I'm trying to write a global hook in C# and C++/CLI. I'm almost done if there wasn't this little delegate problem. I get the function pointer to a delegate and pass it to my...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
7
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
3
by: John Dalberg | last post by:
I have seen examples for List<T>.FindAll(findthis)where findthis is a predicate. How do I pass a parameter to this predicate so that I have different values to search for? I don't want to use...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
7
by: raylopez99 | last post by:
On Aug 16, 3:49 pm, Marc Gravell <marc.grav...@gmail.comwrote: If you cannot understand such a simple post, then you don't understand generic delegate types. Apparently you can only read your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.