473,698 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return values from __events

I am gathering from the documentation that return values from __events are
not illegal but are frowned upon in .NET. If this is the case, does one pass
back values from an event handler via "in/out" or "out" parameters ? Or is
it simply that events are just notifications and are not interested in any
values which event handlers might be able to return ?

If the latter is the case, the event model in .NET appears to have a great
shortcoming. Let's say I have an event in a component which will normally be
occurring but which can be cancelled by a possible event handler before the
event occurs. This appears to me to be a common design idiom, which I have
used in my own non-.NET code. How does one handle this situation using
events in .NET ? I would much prefer my component's event notification to
possibly pick up this fact from some event handler and react accordingly,
but if events aren't supposed to pass back values, either via return values
or "in/out" | "out' values this idiom does not work.
Nov 17 '05 #1
5 2065

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I am gathering from the documentation that return values from __events are
not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via "in/out" or "out" parameters ? Or is
it simply that events are just notifications and are not interested in any
values which event handlers might be able to return ?

If the latter is the case, the event model in .NET appears to have a great
shortcoming. Let's say I have an event in a component which will normally be occurring but which can be cancelled by a possible event handler before the event occurs. This appears to me to be a common design idiom, which I have
used in my own non-.NET code. How does one handle this situation using
events in .NET ? I would much prefer my component's event notification to
possibly pick up this fact from some event handler and react accordingly,
but if events aren't supposed to pass back values, either via return values or "in/out" | "out' values this idiom does not work.
In most cases, you would use a member variable in the EventArgs derived
class you should be passing with the event notification. A proper event
delegate signature should be in the form name(object sender, EventArgs args)
You are pretty much free to do whatever you want in the EventArgs derived
parameter, some Framework code uses a Handled property on that class which
you can set, you could, in theory anyway, extend the class to provide a
mechanism to report back to the event server immediatly(even with another
event!), I can't say if it would be a good idea or not, but you are free to
do so.

Nov 17 '05 #2
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I am gathering from the documentation that return values from
__events are not illegal but are frowned upon in .NET. If this is
the case, does one pass back values from an event handler via
"in/out" or "out" parameters ? Or is it simply that events are just
notifications and are not interested in any values which event
handlers might be able to return ?

If the latter is the case, the event model in .NET appears to have a
great shortcoming. Let's say I have an event in a component which
will normally be occurring but which can be cancelled by a possible
event handler before the event occurs. This appears to me to be a
common design idiom, which I have used in my own non-.NET code. How
does one handle this situation using events in .NET ? I would much
prefer my component's event notification to possibly pick up this
fact from some event handler and react accordingly, but if events
aren't supposed to pass back values, either via return values or
"in/out" | "out' values this idiom does not work.


In most cases, you would use a member variable in the EventArgs
derived class you should be passing with the event notification. A
proper event delegate signature should be in the form name(object
sender, EventArgs args) You are pretty much free to do whatever you
want in the EventArgs derived parameter, some Framework code uses a
Handled property on that class which you can set, you could, in
theory anyway, extend the class to provide a mechanism to report back
to the event server immediatly(even with another event!), I can't say
if it would be a good idea or not, but you are free to do so.


So essentially the suggested signature for an event is:

void MyEvent(System: :Object *,MyDerivedEven tArgs);

I assume that this is just a way of conforming to what the .NET framework
usually uses so that event handlers are used to handling events with this
type of signature, rather than something which is mandated.

I don't mind following it. However I have certain events where I will
manually be calling the event handlers in the delegate chain before the
event occurs and, if an event argument is set to false, it becomes a
notification back to me from the event handler that the event should not
take place, and I also stop calling the rest of the event handlers for that
event. This particular design pattern works well for something I am doing,
and it would be clearer in that case to simply have the __event be
prototyped as returning a 'bool' value for 'continue with the event or not'
than for me to add the 'bool' value to MyDerivedEventA rgs.

That is why I asked the original question. I do not see anything in the CLR
which does not allow a multi-cast event to have a return value. It does say
in the specs for __delegate, which of course __event uses, that the return
value for the delegate is the return value of the last handler, which of
course makes sense. However, in my design pattern for this type of event,
where I am calling the event handlers myself, it seems that I can check the
'bool' return value from each handler, and stop if one of them is 'false'.
Is there anything in the CLR or CLS which prevents this ? If so, I will add
my 'bool' return to MyDerivedEventA rgs but if not it is clearer to the event
handler if the 'bool' value is the return value for my event.
Nov 17 '05 #3

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:e1******** ******@tk2msftn gp13.phx.gbl...
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I am gathering from the documentation that return values from
__events are not illegal but are frowned upon in .NET. If this is
the case, does one pass back values from an event handler via
"in/out" or "out" parameters ? Or is it simply that events are just
notifications and are not interested in any values which event
handlers might be able to return ?

If the latter is the case, the event model in .NET appears to have a
great shortcoming. Let's say I have an event in a component which
will normally be occurring but which can be cancelled by a possible
event handler before the event occurs. This appears to me to be a
common design idiom, which I have used in my own non-.NET code. How
does one handle this situation using events in .NET ? I would much
prefer my component's event notification to possibly pick up this
fact from some event handler and react accordingly, but if events
aren't supposed to pass back values, either via return values or
"in/out" | "out' values this idiom does not work.
In most cases, you would use a member variable in the EventArgs
derived class you should be passing with the event notification. A
proper event delegate signature should be in the form name(object
sender, EventArgs args) You are pretty much free to do whatever you
want in the EventArgs derived parameter, some Framework code uses a
Handled property on that class which you can set, you could, in
theory anyway, extend the class to provide a mechanism to report back
to the event server immediatly(even with another event!), I can't say
if it would be a good idea or not, but you are free to do so.


So essentially the suggested signature for an event is:

void MyEvent(System: :Object *,MyDerivedEven tArgs);

Effectivly.
I assume that this is just a way of conforming to what the .NET framework
usually uses so that event handlers are used to handling events with this
type of signature, rather than something which is mandated.

I don't mind following it. However I have certain events where I will
manually be calling the event handlers in the delegate chain before the
event occurs and, if an event argument is set to false, it becomes a
notification back to me from the event handler that the event should not
take place, and I also stop calling the rest of the event handlers for that event. This particular design pattern works well for something I am doing,
and it would be clearer in that case to simply have the __event be
prototyped as returning a 'bool' value for 'continue with the event or not' than for me to add the 'bool' value to MyDerivedEventA rgs.

That is why I asked the original question. I do not see anything in the CLR which does not allow a multi-cast event to have a return value. It does say in the specs for __delegate, which of course __event uses, that the return
value for the delegate is the return value of the last handler, which of
course makes sense. However, in my design pattern for this type of event,
where I am calling the event handlers myself, it seems that I can check the 'bool' return value from each handler, and stop if one of them is 'false'.
Is there anything in the CLR or CLS which prevents this ? If so, I will add my 'bool' return to MyDerivedEventA rgs but if not it is clearer to the event handler if the 'bool' value is the return value for my event.


It is a pattern that should be followed when possible. Its often clearer to
users because most other events(if not all) follow it. I think FxCop will
complain about events not in that pattern as well.

While returning bool is clearer in a classic sense(C++ or a procedural
language for example), its not generally proper in the .NET strata, and may
not be as clear as it seems. People who have been using the framework for a
long time may be forced to double take due to the unexpected return value.
I've made the same mistake, with an out parameter, it turned out to not be
as straightforward to use as I figured it would be. I suggest sticking with
the existing pattern.
Anyway, to be clear, it should work at the CLR level. I don't know if it is
compliant with the CLS or not, but there is nothing I know of that will
block it from compiling. I do know its against class library guidelines, but
such a discussion is out of scope here. Reposting about that in
microsoft.publi c.dotnet.genera l would likely produce more varied and
interesting replies.
Nov 17 '05 #4
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:e1******** ******@tk2msftn gp13.phx.gbl...
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I am gathering from the documentation that return values from
__events are not illegal but are frowned upon in .NET. If this is
the case, does one pass back values from an event handler via
"in/out" or "out" parameters ? Or is it simply that events are just
notifications and are not interested in any values which event
handlers might be able to return ?

If the latter is the case, the event model in .NET appears to have
a great shortcoming. Let's say I have an event in a component which
will normally be occurring but which can be cancelled by a possible
event handler before the event occurs. This appears to me to be a
common design idiom, which I have used in my own non-.NET code. How
does one handle this situation using events in .NET ? I would much
prefer my component's event notification to possibly pick up this
fact from some event handler and react accordingly, but if events
aren't supposed to pass back values, either via return values or
"in/out" | "out' values this idiom does not work.

In most cases, you would use a member variable in the EventArgs
derived class you should be passing with the event notification. A
proper event delegate signature should be in the form name(object
sender, EventArgs args) You are pretty much free to do whatever you
want in the EventArgs derived parameter, some Framework code uses a
Handled property on that class which you can set, you could, in
theory anyway, extend the class to provide a mechanism to report
back to the event server immediatly(even with another event!), I
can't say if it would be a good idea or not, but you are free to do
so.
So essentially the suggested signature for an event is:

void MyEvent(System: :Object *,MyDerivedEven tArgs);

Effectivly.

I assume that this is just a way of conforming to what the .NET
framework usually uses so that event handlers are used to handling
events with this type of signature, rather than something which is
mandated.

I don't mind following it. However I have certain events where I will
manually be calling the event handlers in the delegate chain before
the event occurs and, if an event argument is set to false, it
becomes a notification back to me from the event handler that the
event should not take place, and I also stop calling the rest of the
event handlers for that event. This particular design pattern works
well for something I am doing, and it would be clearer in that case
to simply have the __event be prototyped as returning a 'bool' value
for 'continue with the event or not' than for me to add the 'bool'
value to MyDerivedEventA rgs.

That is why I asked the original question. I do not see anything in
the CLR which does not allow a multi-cast event to have a return
value. It does say in the specs for __delegate, which of course
__event uses, that the return value for the delegate is the return
value of the last handler, which of course makes sense. However, in
my design pattern for this type of event, where I am calling the
event handlers myself, it seems that I can check the 'bool' return
value from each handler, and stop if one of them is 'false'. Is
there anything in the CLR or CLS which prevents this ? If so, I will
add my 'bool' return to MyDerivedEventA rgs but if not it is clearer
to the event handler if the 'bool' value is the return value for my
event.


It is a pattern that should be followed when possible. Its often
clearer to users because most other events(if not all) follow it. I
think FxCop will complain about events not in that pattern as well.

While returning bool is clearer in a classic sense(C++ or a procedural
language for example), its not generally proper in the .NET strata,
and may not be as clear as it seems. People who have been using the
framework for a long time may be forced to double take due to the
unexpected return value.


Yes, I can understand this. Yet a design pattern which allows an event
handler for an upcoming event to essentially cancel the event, by
'returning' a bool false value, seems not to have been in the consideration
of the .NET designers. In fact the general explanation for reacting to
events is that the event is just a notification and if the event handler
wants to do anything in regard to this event with the object generating the
event, it needs to access the object directly through the sender parameter.
This seems unnecessarily complicated to me. The idea that an event handler
should never affect the event itself through the event handler parameters
seems too simplistic. Of course there is nothing stopping me from designing
my own events. But I really wish that some of the discussions regarding
events in the MSDN doc had acknowledged the fact that event handlers might
want to cancel or alter the event itself.
I've made the same mistake, with an out
parameter, it turned out to not be as straightforward to use as I
figured it would be. I suggest sticking with the existing pattern.
Anyway, to be clear, it should work at the CLR level. I don't know if
it is compliant with the CLS or not, but there is nothing I know of
that will block it from compiling. I do know its against class
library guidelines, but such a discussion is out of scope here.
Reposting about that in microsoft.publi c.dotnet.genera l would likely
produce more varied and interesting replies.


Thanks for your reply. I will probably follow the standard guidelines even
if it goes against my sense of design in this case. I have looked at the CLS
specification and there is nothing there which specifies anything about the
types in the method signature for an event other than that the types
themselves must be CLS compliant. Neverthless, who am I to upset the status
quo <g> . Actually the guidelines are decent as having a structure to pass
event arguments is simpler than having a series of parameters, if less
understandable in general.
Nov 17 '05 #5
<snip>
Yes, I can understand this. Yet a design pattern which allows an event
handler for an upcoming event to essentially cancel the event, by
'returning' a bool false value, seems not to have been in the consideration of the .NET designers. In fact the general explanation for reacting to
events is that the event is just a notification and if the event handler
wants to do anything in regard to this event with the object generating the event, it needs to access the object directly through the sender parameter. This seems unnecessarily complicated to me. The idea that an event handler
should never affect the event itself through the event handler parameters
seems too simplistic. Of course there is nothing stopping me from designing my own events. But I really wish that some of the discussions regarding
events in the MSDN doc had acknowledged the fact that event handlers might
want to cancel or alter the event itself.
There is no particular discussion I know of, something like [1] is the model
I'd use. Also, by allowing all your event properties to be settable, ideally
a event handler could modify the event data easily. However, you should look
into event ordering. As far as I know, there is no guarentee that an event
will be called in any predictable order, without that a handler that changes
events may end up being called last. Unless there is a guarentee its
something I'd be hesitant to rely on.
I've made the same mistake, with an out
parameter, it turned out to not be as straightforward to use as I
figured it would be. I suggest sticking with the existing pattern.
Anyway, to be clear, it should work at the CLR level. I don't know if
it is compliant with the CLS or not, but there is nothing I know of
that will block it from compiling. I do know its against class
library guidelines, but such a discussion is out of scope here.
Reposting about that in microsoft.publi c.dotnet.genera l would likely
produce more varied and interesting replies.


Thanks for your reply. I will probably follow the standard guidelines even
if it goes against my sense of design in this case. I have looked at the

CLS specification and there is nothing there which specifies anything about the types in the method signature for an event other than that the types
themselves must be CLS compliant. Neverthless, who am I to upset the status quo <g> . Actually the guidelines are decent as having a structure to pass
event arguments is simpler than having a series of parameters, if less
understandable in general. Yeah, its a little strange, but allows for proper OO behaviour. You don't
have to pass a concrete derived event args, but you could possibly pass an
extended one that handlers can test for, etc. Its not always advisable, but
it is possible.

Nov 17 '05 #6

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

Similar topics

66
4997
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
15
2799
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
1
4674
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event, if you set this ActionCode to various values the control will respond in various ways (reject, reject change field, accept, etc). I am trying to understand exactly how that works as I am trying to extend the control and add more processing s...
0
1199
by: | last post by:
I am trying to use my unmanaged c++ class library in managed classes. So far everything seems to be ok, just passing on the __events from unmanaged to managed code using the __hook functions is a mising link. I am getting this code: error C3731: incompatible event 'void sLib::CDataset::OnSetProgress(sLib::CProgressInfo &)' and handler 'void SkyscanLibBase::CDataset::DoLocalProgress(sLib::CProgressInfo &)'; event source and event handler...
16
17399
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
43
3358
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In this case, just zeros (though I just to prove whatever ends up working, having a counting generator would be nice). The target syntax would be something like >>> a,b,c = zeros() >>> q,r,s,t,u,v = zeros()
17
2031
by: binjobster | last post by:
Hello everyone, I'm updating some documents that describes so many C/C++ functions/method(about 2500). But I'm not familiar with these codes. So I want to find such a utility can generate returning value list from the codes. Is it possible? Or is it hard to write one? Thanks.
8
5201
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return more than one value, for example three int-s, I would have to change my "logic" and pass the references to my
4
7137
by: Jonathan | last post by:
I have a SQL stored procedure for adding a new record in a transactions table. It also has two return values: CounterID and IDKey. I want to create a webservice that accepts the 10 input parameters and returns the two return values. My C# programmer here says that webservice methods can only return 1 value per method. Is that right? Though I haven't ever created a webservice, I would have thought that a method could return a whole lot...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4369
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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 we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.