473,396 Members | 1,780 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,396 software developers and data experts.

when to use delegates and events

Hello,

I have a question about delegates and events

I have a basic understanding of each, but in the apps I'm building I'm
never needing to use these tools. I'm wondering when and where you
should use these features. I am assuming that they are used in your
presentation layer but perhaps I am sorely wrong.

thank you

Sean

Nov 17 '05 #1
4 1712
DKode,
a delegate is a reference type that provides a generic reference to a
method, with a specific return type and argument list. Delegates are
user-defined types, so when you define a delegate, you are actually defining
a particular type of delegate, not a specific instance.

Your delegate can represent any method at all, provided it has the same
argument list and return type.

Delegates are widely used in .NET -- in multithreaded programming, to
specify the method that is called when a thread is started. They are used
as callbacks in asynchronous programming. Any class or object that has a
"BeginXXX" method is using a delegate.

I frequently use delegates (event delegates) to assist in returning
information from one form to a parent form when it is closed. The parent
form simply becomes an event "subscriber". There are many other uses.
HTH
Peter

"DKode" <dk****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hello,

I have a question about delegates and events

I have a basic understanding of each, but in the apps I'm building I'm
never needing to use these tools. I'm wondering when and where you
should use these features. I am assuming that they are used in your
presentation layer but perhaps I am sorely wrong.

thank you

Sean

Nov 17 '05 #2
Events are not only used in the UI layer, but everywhere where feedback
needs to be provided.

Events provide a way for the object to call back to the main code and
indicates that something has happen where action might need to be taken.

Let's look at things from a human perspective. Suppose that you have a dam
holding back water. You want to make sure that the dam does not break under
pressure. Well, the operator could look at the instrumentation every minute
and compare the value with the expected pressure, but the operator probably
has better things to do. And besides, how often would the operator look at
the instruments -- every minute, or 30 seconds, or sooner.

Now, if the dam was event driven, the dam could have sensors located across
its surface. These sensors would monitor the pressure. If the pressure
gets too high, the sensors will send a signal back to the operator and maybe
flash a light and sound an alert. This will get the operators attention so
the person can open the gates.

The operator looking at the console would be like a program running through
a look or using a thread that wakes periodically. Instead of that, let's
turn the sensors into events. When an event determines the pressure is too
high (PressureOutOfBounds event), the event will create an event delegate
(PressureOutOfBoundsEventHandler delegate) that points to a method that will
sound an alert on the console. The handler receives some parameters
(PressureOutOfBoundsEventArgs) which might contain information like the
current pressure.
"DKode" <dk****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hello,

I have a question about delegates and events

I have a basic understanding of each, but in the apps I'm building I'm
never needing to use these tools. I'm wondering when and where you
should use these features. I am assuming that they are used in your
presentation layer but perhaps I am sorely wrong.

thank you

Sean

Nov 17 '05 #3
One approach is to look at using a delegate when you encounter a
situation
where you would use a callback in C++. So a delegate is a type safe
function
pointer. So a method that does some work can be passed a delegate as a
parameter and when the work is done the method can "callback" the caller
using
the delegate and notify the caller that the work is done. This is much
more
efficient than "polling" where the caller would continue to check to see
if the
work was done at set intervals. Polling can eat up a lot of network
bandwidth.

An event is used when you want to have zero or more listeners register
an
interest in an action. When the action occurs the broadcaster can notify
the
registered listeners of the action. In a sense, the user of your class
can "extend"
the behaviour of your class by registering an interest in an event that
your class
fires.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
Delegates are evolved from "function pointers", which when mastered allow a
fundamental increase in the capability a programmer has to design elegant and
powerful object-oriented systems. Function pointers are really what makes OOP
possible.

With proper use of delegates, you can reduce object interdependancy... One
object can service a series of events without ever having direct knowledge of
the other objects who have attached to those events... allowing you to
enhance maintanence, improve extensibility, and reusability of your code.

As a general rule, remember that if you attach a handler to an event
delegate, you should always remove that handler from the event delegate when
your object is disposed.

"DKode" wrote:
Hello,

I have a question about delegates and events

I have a basic understanding of each, but in the apps I'm building I'm
never needing to use these tools. I'm wondering when and where you
should use these features. I am assuming that they are used in your
presentation layer but perhaps I am sorely wrong.

thank you

Sean

Nov 17 '05 #5

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

Similar topics

3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
2
by: al | last post by:
Greetings, Can someone please tell me the what is the defference betweeen delegates and events handler??? MTIA, Grawsha
6
by: Jon Davis | last post by:
I've used delegates fairly heavily for several years in C# for event handling and for starting threads. Does anyone have any real-world scenarios where delegates were both extremely useful and...
22
by: dvestal | last post by:
Suppose I have this: class C { public delegate void MyEventHandler(); public event MyEventHandler MyEvent; public void foo() { MyEvent(); // NullReferenceException? } }
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
1
by: Reg | last post by:
Hello, I have a situation where a text based protocol comes to my component to be handled. Protocol has differnt commands and they have different arguments. The question is that which is in C#...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
5
by: puzzlecracker | last post by:
I took a closer look at delegates and events, and it became apparent that events don't offer any extra functionalities over delegates. I don't even see it as syntactical sugar over "overtly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.