Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Does event return anything?
If yes,then what it returns?
Posted
Updated 8-Apr-11 16:49pm
v2
Comments
[no name] 8-Apr-11 1:26am    
be specific what you want and from where?
Sergey Alexandrovich Kryukov 8-Apr-11 22:51pm    
It's clear. Also the question is not that trivial as you may think.
Please see my Answer.

I'll vote 5 for the Question, even though it looks naive. For many not everything is clear about it.
I only had to improve the tag and the title: it's about .NET, nothing to do with ASP.NET.
--SA

Let me Google it for you[^]

Please don't be so lazy and try to read and find it by yourself. If you are still confused then do tell us with effort that what is the issue.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 8-Apr-11 22:48pm    
Sandeep, I would vote 5 if you removed ASP.NET from the search. The Question has nothing to do with .NET. By the way, it's more interesting then it seems.
Please see my Answer: pretty interesting I hope :-)
--SA
Sandeep Mewara 9-Apr-11 0:38am    
:) Hmmm... done Events in .NET

BTW, 5 to you and Albein.
Sergey Alexandrovich Kryukov 9-Apr-11 0:57am    
Thank you very much, Sandeep.
--SA
Sandeep Mewara 9-Apr-11 1:00am    
Ok. :)
Sergey Alexandrovich Kryukov 9-Apr-11 1:13am    
No-no, it was a wrong post from me, Sandeep, sorry.
You might get in it e-mail, but it should have beed fixed by the moment you reply.
Thank you very much.
--SA
Hi

The event raises, not returns
mostly the event handler's also doesn't return anything. Because the event subscribed by many subscriber then the event supposed to receive multiple values in random order. Even the event can't identify who is return the value. The handling is decoupled through a delegate. So it is useless for the event to receive values and it is discouraged. So the event handler signature comes with the "void".

But you can set your global variables from the event handling code.
 
Share this answer
 
Comments
Nish Nishant 8-Apr-11 10:13am    
Voted 5, proposed as answer.
Albin Abel 8-Apr-11 11:28am    
Thanks Nishant
Tarun.K.S 8-Apr-11 14:17pm    
Good answer and well explained! high-5! :D
Albin Abel 8-Apr-11 23:10pm    
Thanks Tarun.K.S
Tarun.K.S 9-Apr-11 13:29pm    
You can call me Tarun!
Does not return. You can raise an event.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Apr-11 22:45pm    
There is certain sense in the question. There is some more to it.
Anyway, I'll vote 5 for this Answer, technically speaking it is correct.
Please see my Answer.
--SA
In addition to correct Answer by Albin and in response to partially correct Answer by Abhinav:

Standard event handling requires event arguments to be

C#
delegate EventDelegateType(object sender, EventArgumentType eventArgs);

where EventArgumentType is derived from System.EventArgs; in this case the event itself is to be declared as System.EventHandler (one type is generic with generic type parameter of System.EventArgs or derived) or a base EventHandler.

Only this type will pass the check by FxCop.

In this standard schema, even the event handler cannot return value. Modification of event argument is very important and in certain sense carry "return" functionality, but it is not the return, technically.

What happens if we neglect this standard recommendation and create a custom event type with return value?
It won't be very helpful. The event invocation ignored return values. Why? Just to keep things more simple. In the multi-cast situation (which is usual for event; everyone can add another handler to the same event), there are several different returns. What do to with that? Microsoft decided to ignore them.

Just for events! For delegate instances, anyone can get Delegate.GetInvocationList, get all the return values and do something different. I practice this for certain situations.

--SA
 
Share this answer
 
v2
Comments
Albin Abel 8-Apr-11 23:12pm    
Deserved 5+ for this detailed one.
Sergey Alexandrovich Kryukov 8-Apr-11 23:19pm    
Thank you, Albin.
--SA
Sandeep Mewara 9-Apr-11 0:38am    
My 5+ to it.
Sergey Alexandrovich Kryukov 9-Apr-11 1:04am    
Thank you, Sandeep. A bit of interesting delicacy on this simple topic, isn't it?
--SA
Sandeep Mewara 9-Apr-11 2:37am    
To be true, you made it interesting. :)
Events don't return anything.
The method that handles the event can return what you want it to.
 
Share this answer
 
Comments
Albin Abel 8-Apr-11 9:35am    
Usually event handlers also doesn't return anything :).
Sergey Alexandrovich Kryukov 8-Apr-11 22:43pm    
The Question is actually more interesting.
Please see my Answer -- I think it is comprehensive.
--SA
Abhinav S 9-Apr-11 6:29am    
There are custom events that could be written to return parameters, right?
Sergey Alexandrovich Kryukov 8-Apr-11 22:31pm    
Abhinav, event handler also does not return anything, in most cases.
If you follow standard event recommendation (which FxCop passes) it never does.
There is a good reason for that: event instance ignores return because in multi-cast situation (which is usual for event), there are several different returns.
Please see my Answer which explains the details.
--SA
Abhinav S 9-Apr-11 6:29am    
There are custom events that could be written to return parameters, right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900