473,810 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing events as arguments to functions

It seems like there should be a way to pass events around like
delegates but on the other hand they’re sort of special delegates
because the only one allowed to invoke them is the owner of the event.
What I want to do is create a generic event forwarder that forwards
events from a server to a client. It will only subscribe to the
server events once and only if there’s at least one client
subscriber. Once the last client subscriber removes its event handler
then the forwarder will remove its event handler from the server.

To use this class it would look something like this:
m_Forwarder = new GenericEventFor warder<MyArgs>( (object)this);
// pass this pointer so forwarder can use as sender of events so
clients think that we're the sender

public event EventHandler<My ArgsSomethingCh anged
{
Add
{
m_Forwarder.Add (value, new
EventHandler<My Args>(m_Server. SomethingChange d));
}
Remove
{
m_Forwarder.Rem ove(value, new
EventHandler<My Args>(m_Server. SomethingChange d));
}
}

The forwarder class would maintain it’s own delegate chain via
Delegate.Combin e() and Delegate.Remove () keeping track of the count of
delegates.

Sep 17 '08 #1
2 1595
Israel <is**********@h otmail.comwrote :
It seems like there should be a way to pass events around like
delegates but on the other hand they=3Fre sort of special delegates
because the only one allowed to invoke them is the owner of the event.
They're not delegates at all. They're a pair of methods - subscribe and
unsubscribe. Talking about passing them around is like talking about
passing a property around (rather than the *value* of a property). You
can of course pass a PropertyInfo (or an EventInfo) if necessary.
What I want to do is create a generic event forwarder that forwards
events from a server to a client. It will only subscribe to the
server events once and only if there=3Fs at least one client
subscriber. Once the last client subscriber removes its event handler
then the forwarder will remove its event handler from the server.
That sounds feasible. In that case, the EventInfo would actually give
you everything you need - admittedly you'd be calling the
subscribe/unsubscribe bits by reflection (unless you created a delegate
to do that - very confusing!) but it would do the trick.
<snip>
The forwarder class would maintain it=3Fs own delegate chain via
Delegate.Combin e() and Delegate.Remove () keeping track of the count of
delegates.
Just use += and -= as shortcuts or Combine and Remove - and when it
returns null, that means the last subscriber has been removed. No need
to do explicit counting.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #2
On Sep 17, 3:57*pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
That sounds feasible. In that case, the EventInfo would actually give
you everything you need - admittedly you'd be calling the
subscribe/unsubscribe bits by reflection (unless you created a delegate
to do that - very confusing!) but it would do the trick.
I guess I was originally thinking I wanted to avoid using reflection
due to the volume of events but since it would only be used for
subscribe / unsubscribe, and that won't happen that often, maybe it
wouldn't be such a bad solution. I always feel dirty using reflection
kind of like using IDispatch; I need to take a shower after using
either in code.

Sep 17 '08 #3

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

Similar topics

4
6378
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in outcome between: function add_some_extra(&$string) { $string .= 'and something extra.'; }
3
14962
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
12
2811
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of function descriptors for the overloaded functions of that name. A function descriptor would contain the address of the function to be called, and a description of the parameters that it must take. b. A list of parameters. This would be compared to the...
5
2359
by: knocte | last post by:
Hello. I am a web developer very worried about "bloat code" and "languages mixture". So, since some time, I always try to avoid completely the use of javascript in XHTML/HTML files. This leads to me to hate, also, any event (onload, onchange, ...) instanciated in the element tag itself, like: <input type="text" onkeyup="return doSomething();" /> I would replace this code above with an input element marked with an
6
3955
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class takes that form object, shows it with showdialog and the basically passes the control to the form...
4
1514
by: 63q2o4i02 | last post by:
Hi, I'm writing a hand-written recursive decent parser for SPICE syntax parsing. In one case I have one function that handles a bunch of similar cases (you pass the name and the number of tokens you're looking for). In another case I have a function that handles a different set of tokens and so it can't use the same arguments as the first one, and in fact takes no arguments. However, these functions are semantically similar and are...
10
13062
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
2
1093
by: PatrickMinnesota | last post by:
I've been reading the docs and looking for an answer and seem stuck. I'm either not looking in the right places or not understanding what I'm reading. I have a bunch of functions. I want to put them in a list. Then I want to pass that list into another function which does some setup and then loops through the list of passed in functions and executes them. Some of them need arguments passed in too. Can someone point me to where to...
1
4911
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the events when click on that date that perticular event displyed in a text box.But my requirement is to when click on that date that related event displyed in same td row not the text box. and also i add the events to that calendar.plz advice how to...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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
10379
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...
1
10393
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
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
6882
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4334
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
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.