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

Anonymous use of events

Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet

I'd like to make publish/subscribe scenario where the publishing object is a couple of layers removed from the subscriber. The publisher fires an event and doesn't know who (if anyone) is listening. The subscriber, on the other hand, wants the information, but doesn't want to know who published it (to fully decouple the two from one another)

Publisher -->> Fires Even
ObjectList (contains subscriber objects) --> SubscriberObject (receives event).

Do I need to keep the event as a public static member of the Publisher class or is there a way that I can pass it down to subscriber. I can't pass an event as a parameter. Am I looking at this the wrong way

Thanks
George
Nov 16 '05 #1
7 2169
Hi,

From what I read so far, I can't tell what the problem is -
what's wrong with using the standard eventhandler for handling the events?

Or you want to subscribe a bunch of "clients" to a single instance of that
publisher?

Branimir.

--
Branimir Giurov
MCSD.NET, MCDBA, MCT
eAgility LLC

"George Carl" <an*******@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet.
I'd like to make publish/subscribe scenario where the publishing object is a couple of layers removed from the subscriber. The publisher fires an
event and doesn't know who (if anyone) is listening. The subscriber, on the
other hand, wants the information, but doesn't want to know who published it
(to fully decouple the two from one another).
Publisher -->> Fires Event
ObjectList (contains subscriber objects) --> SubscriberObject (receives event).
Do I need to keep the event as a public static member of the Publisher class or is there a way that I can pass it down to subscriber. I can't pass
an event as a parameter. Am I looking at this the wrong way?
Thanks!
George

Nov 16 '05 #2
I'd like to subscribe a bunch of clients to the single instance of the publisher. In order to use the standard event handler, I need to declare where the Event "lives". That is

Publisher.myEvent += new Publisher.MyEventHandler (this.LocalFunction)

I'd rather not reveal which instance of Publisher I'm subscribing to. That is, I'm happy knowing that I have a phone line from the phone company, but I don't care which phone company it is that makes my telephone ring. Does that make sense

Thanks
Georg

----- Branimir Giurov wrote: ----

Hi

From what I read so far, I can't tell what the problem is
what's wrong with using the standard eventhandler for handling the events

Or you want to subscribe a bunch of "clients" to a single instance of tha
publisher

Branimir

--
Branimir Giuro
MCSD.NET, MCDBA, MC
eAgility LL

"George Carl" <an*******@discussions.microsoft.com> wrote in messag
news:1C**********************************@microsof t.com..
Perhaps this is a dumb question, but I've tried Google and the newsgroups

and can't figure out a satisfactory answer yet
I'd like to make publish/subscribe scenario where the publishing object i a couple of layers removed from the subscriber. The publisher fires a
event and doesn't know who (if anyone) is listening. The subscriber, on th
other hand, wants the information, but doesn't want to know who published i
(to fully decouple the two from one another) Publisher -->> Fires Even

ObjectList (contains subscriber objects) --> SubscriberObject (receive

event)
Do I need to keep the event as a public static member of the Publishe class or is there a way that I can pass it down to subscriber. I can't pas
an event as a parameter. Am I looking at this the wrong way Thanks

Georg


Nov 16 '05 #3
On Tue, 6 Apr 2004 22:11:02 -0700, "George Carl"
<an*******@discussions.microsoft.com> wrote:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet.

I'd like to make publish/subscribe scenario where the publishing object is a couple of layers removed from the subscriber. The publisher fires an event and doesn't know who (if anyone) is listening. The subscriber, on the other hand, wants the information, but doesn't want to know who published it (to fully decouple the two from one another).

Publisher -->> Fires Event
ObjectList (contains subscriber objects) --> SubscriberObject (receives event).

Do I need to keep the event as a public static member of the Publisher class or is there a way that I can pass it down to subscriber. I can't pass an event as a parameter. Am I looking at this the wrong way?

Thanks!
George


Hi George,

I faced more or less the same problem this week, only more so, since I
needed several "publishers" to fire the exact same event, the
"subscriber" not caring where the event comes from. I came up with the
following workaround, which does it for me.

I have a separate claas library, called "KioskEvents" (Kiosk being the
general name I use for this software project). This library contains
all events, EventArgs-derived classes and event delegate declarations
used throughout the solution. For each event, there is a public method
that actually fires the event. This method can be called from anywhere
in the solution (provided the component references the KioskEvents
class library).

Here is the meat of the events class library, providing for just one
event. This event passes a string to the subscriber. Note that in this
setup, the subscriber can still tell who the original publisher is:
it's right there in the "sender" parameter.
I have tried to make this code readable in a newsreader by using
spaces instead of tabs; use a fixed font such as courier to read this
:)
namespace kiosk
{
////////////////////////////////
// Event Arguments
////////////////////////////////

public class CEventArgsString : EventArgs
{
public string strData ;
}

///////////////////////////////
// Event Delegates
//////////////////////////////

public delegate void EventHandlerString
( Object sender, CEventArgsString eString );

public class CKioskEvents
{
////////////////////////////
// Events
////////////////////////////

public static event EventHandlerString eventNewStation;

////////////////////////////
// Event-raising methods
////////////////////////////

public static void RaiseEventNewStation(
object sender, string strStation )
{
if( eventNewStation != null )
{
CEventArgsString eString = new CEventArgsString( );
eString.strData = strStation ;
eventNewStation( sender, eString );
}
}
} // CKioskEvents
} // kiosk namespace
So, to raise the event, the publisher would call

CKioskEvents.RaiseEventNewStation( this, strStationName );

To receive the event, the subscriber would have something like

CKioskEvents.eventNewStation +=
new EventHandlerString( OnNewStation );

and of course a handler method called OnNewStation:

void OnNewStation( object sender, CEventArgsString e )
{
string strSender = sender.ToString( );
string strStation = e.strData ;
}
Hope this helps / makes sense !
Jan Roelof

Nov 16 '05 #4

"George Carl" <an*******@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
I'd like to subscribe a bunch of clients to the single instance of the
publisher. In order to use the standard event handler, I need to declare
where the Event "lives". That is:

Publisher.myEvent += new Publisher.MyEventHandler (this.LocalFunction).

I'd rather not reveal which instance of Publisher I'm subscribing to.
That is, I'm happy knowing that I have a phone line from the phone
company, but I don't care which phone company it is that makes my
telephone ring. Does that make sense?

So, how do yo uwant to subscribe to the event? Do you want to call a method?
Or subscribe to an event on a sort of EventPublisherManager which then
decides which specific Publisher tosubscribe to?
Thanks,
George

----- Branimir Giurov wrote: -----

Hi,

From what I read so far, I can't tell what the problem is -
what's wrong with using the standard eventhandler for handling the
events?

Or you want to subscribe a bunch of "clients" to a single instance of
that
publisher?

Branimir.

--
Branimir Giurov
MCSD.NET, MCDBA, MCT
eAgility LLC

"George Carl" <an*******@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
> Perhaps this is a dumb question, but I've tried Google and the

newsgroups,
and can't figure out a satisfactory answer yet.
>> I'd like to make publish/subscribe scenario where the publishing object is
a couple of layers removed from the subscriber. The publisher fires
an
event and doesn't know who (if anyone) is listening. The subscriber,
on the
other hand, wants the information, but doesn't want to know who
published it
(to fully decouple the two from one another). >> Publisher -->> Fires Event

> ObjectList (contains subscriber objects) --> SubscriberObject

(receives
event).
>> Do I need to keep the event as a public static member of the Publisher
class or is there a way that I can pass it down to subscriber. I
can't pass
an event as a parameter. Am I looking at this the wrong way? >> Thanks!

> George


Nov 16 '05 #5
I'd like to subscribe a bunch of clients to the single instance of the publisher. In order to use the standard event handler, I need to declare where the Event "lives". That is

Publisher.myEvent += new Publisher.MyEventHandler (this.LocalFunction)

I'd rather not reveal which instance of Publisher I'm subscribing to. That is, I'm happy knowing that I have a phone line from the phone company, but I don't care which phone company it is that makes my telephone ring. Does that make sense

Thanks
Georg

----- Branimir Giurov wrote: ----

Hi

From what I read so far, I can't tell what the problem is
what's wrong with using the standard eventhandler for handling the events

Or you want to subscribe a bunch of "clients" to a single instance of tha
publisher

Branimir

--
Branimir Giuro
MCSD.NET, MCDBA, MC
eAgility LL

"George Carl" <an*******@discussions.microsoft.com> wrote in messag
news:1C**********************************@microsof t.com..
Perhaps this is a dumb question, but I've tried Google and the newsgroups

and can't figure out a satisfactory answer yet
I'd like to make publish/subscribe scenario where the publishing object i a couple of layers removed from the subscriber. The publisher fires a
event and doesn't know who (if anyone) is listening. The subscriber, on th
other hand, wants the information, but doesn't want to know who published i
(to fully decouple the two from one another) Publisher -->> Fires Even

ObjectList (contains subscriber objects) --> SubscriberObject (receive

event)
Do I need to keep the event as a public static member of the Publishe class or is there a way that I can pass it down to subscriber. I can't pas
an event as a parameter. Am I looking at this the wrong way Thanks

Georg


Nov 16 '05 #6
On Tue, 6 Apr 2004 22:11:02 -0700, "George Carl"
<an*******@discussions.microsoft.com> wrote:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet.

I'd like to make publish/subscribe scenario where the publishing object is a couple of layers removed from the subscriber. The publisher fires an event and doesn't know who (if anyone) is listening. The subscriber, on the other hand, wants the information, but doesn't want to know who published it (to fully decouple the two from one another).

Publisher -->> Fires Event
ObjectList (contains subscriber objects) --> SubscriberObject (receives event).

Do I need to keep the event as a public static member of the Publisher class or is there a way that I can pass it down to subscriber. I can't pass an event as a parameter. Am I looking at this the wrong way?

Thanks!
George


Hi George,

I faced more or less the same problem this week, only more so, since I
needed several "publishers" to fire the exact same event, the
"subscriber" not caring where the event comes from. I came up with the
following workaround, which does it for me.

I have a separate claas library, called "KioskEvents" (Kiosk being the
general name I use for this software project). This library contains
all events, EventArgs-derived classes and event delegate declarations
used throughout the solution. For each event, there is a public method
that actually fires the event. This method can be called from anywhere
in the solution (provided the component references the KioskEvents
class library).

Here is the meat of the events class library, providing for just one
event. This event passes a string to the subscriber. Note that in this
setup, the subscriber can still tell who the original publisher is:
it's right there in the "sender" parameter.
I have tried to make this code readable in a newsreader by using
spaces instead of tabs; use a fixed font such as courier to read this
:)
namespace kiosk
{
////////////////////////////////
// Event Arguments
////////////////////////////////

public class CEventArgsString : EventArgs
{
public string strData ;
}

///////////////////////////////
// Event Delegates
//////////////////////////////

public delegate void EventHandlerString
( Object sender, CEventArgsString eString );

public class CKioskEvents
{
////////////////////////////
// Events
////////////////////////////

public static event EventHandlerString eventNewStation;

////////////////////////////
// Event-raising methods
////////////////////////////

public static void RaiseEventNewStation(
object sender, string strStation )
{
if( eventNewStation != null )
{
CEventArgsString eString = new CEventArgsString( );
eString.strData = strStation ;
eventNewStation( sender, eString );
}
}
} // CKioskEvents
} // kiosk namespace
So, to raise the event, the publisher would call

CKioskEvents.RaiseEventNewStation( this, strStationName );

To receive the event, the subscriber would have something like

CKioskEvents.eventNewStation +=
new EventHandlerString( OnNewStation );

and of course a handler method called OnNewStation:

void OnNewStation( object sender, CEventArgsString e )
{
string strSender = sender.ToString( );
string strStation = e.strData ;
}
Hope this helps / makes sense !
Jan Roelof

Nov 16 '05 #7

"George Carl" <an*******@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
I'd like to subscribe a bunch of clients to the single instance of the
publisher. In order to use the standard event handler, I need to declare
where the Event "lives". That is:

Publisher.myEvent += new Publisher.MyEventHandler (this.LocalFunction).

I'd rather not reveal which instance of Publisher I'm subscribing to.
That is, I'm happy knowing that I have a phone line from the phone
company, but I don't care which phone company it is that makes my
telephone ring. Does that make sense?

So, how do yo uwant to subscribe to the event? Do you want to call a method?
Or subscribe to an event on a sort of EventPublisherManager which then
decides which specific Publisher tosubscribe to?
Thanks,
George

----- Branimir Giurov wrote: -----

Hi,

From what I read so far, I can't tell what the problem is -
what's wrong with using the standard eventhandler for handling the
events?

Or you want to subscribe a bunch of "clients" to a single instance of
that
publisher?

Branimir.

--
Branimir Giurov
MCSD.NET, MCDBA, MCT
eAgility LLC

"George Carl" <an*******@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
> Perhaps this is a dumb question, but I've tried Google and the

newsgroups,
and can't figure out a satisfactory answer yet.
>> I'd like to make publish/subscribe scenario where the publishing object is
a couple of layers removed from the subscriber. The publisher fires
an
event and doesn't know who (if anyone) is listening. The subscriber,
on the
other hand, wants the information, but doesn't want to know who
published it
(to fully decouple the two from one another). >> Publisher -->> Fires Event

> ObjectList (contains subscriber objects) --> SubscriberObject

(receives
event).
>> Do I need to keep the event as a public static member of the Publisher
class or is there a way that I can pass it down to subscriber. I
can't pass
an event as a parameter. Am I looking at this the wrong way? >> Thanks!

> George


Nov 16 '05 #8

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

Similar topics

20
by: Doug Holton | last post by:
Is there any metaclass trick or something similar to allow anonymous code blocks? I'd like to be able to let users do something like this fictitious example: b = Button() b.OnClick =: print...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
2
by: Pelle Beckman | last post by:
Does C++ in some way support anonymous functions? Say I have a callback function that I register with SetCallback() (this perticular example is from my attempts at messages, signals, events, etc...
1
by: msnews.microsoft.com | last post by:
Once the form is activated I would like to remove the reference to the anonymous delegate. Can I reference the delegate from within the delegate? For example ... form.Activated += delegate {...
7
by: George Carl | last post by:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet I'd like to make publish/subscribe scenario where the publishing object is...
9
by: John Smith | last post by:
I really can not appreciate why Microsoft has introduced Anonymous methods. It promotes quick and dirty style of programming and as I can see it offers no advantages over normal methods. I have...
22
by: PJ6 | last post by:
I just learned about anonymous methods and was taken aback to discover that they are only available in C#. What, is there still a stigma against VB.Net, that maybe somehow this is a language that...
0
by: Coder Guy | last post by:
I am trying to subscribe to an event using reflection. The guarantee is that the the event delegate will be of type EventHandler or at least the e parameter inherits from EventArgs. Hard coding...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.