473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why are event delegates required ?

62 New Member
Hello VB.NET Experts.
I am new to VB.NET (using VS2008 on WinXP 32bit).

I was going through tutorials and found the topic of Events and Delegates. However, I fail to understand why should this be required ? Instead of having to raise events, can't one just call the necessary procedure ?

Sorry for posing this basic question. Any replies or even links to existing posts are welcome. [Also not sure if this question strictly falls under VB.NET category.]

Thanks in advance,
Parag Doke
May 26 '09 #1
4 1644
tlhintoq
3,525 Recognized Expert Specialist
Think of an event as a man with a very loud P.A. system. When something happens he announces it to the world. He doesn't really care who's listening or what they do with that information. When he yells out "I've Started", 200 people can react at the same time.

Think of calling each method as a man with a single line telephone and a long list of people to call along with a list of instructions for each person. Now when he makes an announcement he calls each person individually and tells them what they are responsible for doing. He calls DJ and says "You play music". Then he calls Logger and says "You make a note of the time". Then he calls Progress and says "Open that graph". Then he calls Chime to say "Play the happy noise". And while he is making is 200 individual phone calls, he is too busy to do anything else. And these 200 people on his list each have to wait until the previous one gets told.

If you raise an event you don't know or care which classes are listening. You don't know or care how they are going to respond. You distribute the burden to each class. You can add new reactions by writing one small class that subscribes to the event.

Events give you a way to make things happen across classes without the calling class having to know anything about the listening class. It helps you compartmentalize.

For example:
Your very long LookForDuplicates() method can simply raise an event of ProgressUpdate... It doesn't have to know or care what other classes are listening. One of them can be a ProgressBar class. Later you can add a PlayBackgroundMusic class without ever changing your known working primary method of LookForDuplicates()

Events also give you a way to do things Asynchronously. If you
MethodAlpha();
MethodBravo();
MethodCharlie();


MethodCharlie doesn't start until MethodBravo finishes. But if you raise events the program flow continues. Your very time consuming MethodBravo doesn't hold up MethodCharlie.

How about this example:
Picture that you have written a program that does lots of things, has lots of windows and lots of tools. One of them is a single floating pallet with a Stop sign graphic. When you press it, everything needs to stop.

You *could* give your Stop button knowledge of every tool and window and operation. When you add new features, you add code to the Stop button. It has to understand ALL other features and windows. So it becomes a mile long.

Or it could simply raise an event of EmergencyStopEvent, to which all the windows subscribe. It starts and stays about 10 lines long because it does one little thing: It yells out STOP!

Later when you write a new tool and 6 new windows you don't need to go back into your EmergencyStop pallet and update it (without breaking it). Each of the new tools and windows need only to subscribe to the EmergencyStop event.
May 26 '09 #2
paragpdoke
62 New Member
Hello tlhintoq.
Thank you so much for taking out the time to explain this to me in simple terms ! I got the concept beyond doubt. Many many thanks.
Regards,
Parag Doke
May 27 '09 #3
PRR
750 Recognized Expert Contributor
:) really good ans....
May 27 '09 #4
r035198x
13,262 MVP
More formally, the world we model in OOP consists of Observers and observables. Observers can themselves be observables too. When an observable changes state, observers should respond to that change in state by performing an action (possibly a nothing action).
The event handling model is a facility for registering observers to their observables in an efficient way.
Delegates facilitate event handling by providing one name for a group of methods(events) making it possible to programmatically change the method that is run and also plug new code into existing classes.
Jun 2 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
2
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely...
2
by: Marinos Christoforou | last post by:
Sorry if this has been asked before but as an inexperienced wanna-be C# programmer I wondering how to code classes to help build a standard Windows UI. For example to build a common toolbar. I...
3
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
1
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the...
5
by: Paul Bromley | last post by:
Can someone give me a very simple example on how to do this? As an example I have a commaned button in a user control. Once this user control is placed on a form I want to be able to respond in the...
31
by: Scott M. | last post by:
Am I correct in thinking that because C# doesn't have the "Handles" keyword that VB .NET does, we have to register event delegates manually in C#, whereas in VB .NET using "Handles" takes care of...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.