471,348 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Event Subscribers

Tim
Hi,

I have a module that acts as a publisher of events. The clients subscribe
for the events using the '+=' operator. Instead, I would like the clients to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?
Dec 16 '05 #1
6 1608
Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?

Dec 16 '05 #2
Tim
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.


"Marc Gravell" wrote:
Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?


Dec 16 '05 #3
Fair enough answer...

I'm not too familiar with hooking up COM (or other unmanaged) clients to
..Net events (or some similar solution), so now that we all properly
understand the question, I'll duck out (I don't want to confuse things).

Marc
"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much
simpler
to ask them to make a method call. Well, I am not sure about the
complexities
that may be involved in the process - which I am just trying to figure
out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.


"Marc Gravell" wrote:
Well, technically yes it would be possible, by making the
RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't
enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
> Hi,
>
> I have a module that acts as a publisher of events. The clients
> subscribe
> for the events using the '+=' operator. Instead, I would like the
> clients
> to
> call a method like "RegisterForXEvent" passing the required
> information.
> Inside that method, I would like to add the client to the list of
> subscribers. Is it possible to implement the above. If so, how do I do
> it?
> What kinda information should the client pass to the Event publisher?


Dec 16 '05 #4
Tim wrote:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.
If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy

"Marc Gravell" wrote:

Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@micros oft.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?


Dec 17 '05 #5
Tim wrote:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.
If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy
"Marc Gravell" wrote:

Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@micros oft.com...
Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?


Dec 17 '05 #6
Tim
Hi,

Actually, I am interested in knowing if there is a way for Clients to
subscribe for events by making a method call. If so, what is the information
that needs to be provided - i.e. the signature of the method that should be
exposed by the event publisher.

"Andreas Mueller" wrote:
Tim wrote:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.

If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy

"Marc Gravell" wrote:

Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:2C**********************************@micros oft.com...

Hi,

I have a module that acts as a publisher of events. The clients
subscribe
for the events using the '+=' operator. Instead, I would like the clients
to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?

Dec 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pavils Jurjans | last post: by
4 posts views Thread by Greg Patrick | last post: by
1 post views Thread by alexis rzewski | last post: by
6 posts views Thread by sjoshi | last post: by
1 post views Thread by =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post: by
4 posts views Thread by =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post: by

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.