For a static event, should sender be null?

  • Thread starter news-server.san.rr.com
  • Start date
N

news-server.san.rr.com

I have a class that defines a *static* event:

| public static event FubarEventHandler FubarAdded;


This event is raised in public static methods, such as:


| public static void AddFubar(...)
| {
| ...
|
| // Advise that a Fubar was added to the master Fubar list
| if (FubarAdded != null)
| FubarAdded(null, new FubarEventArgs(fubarObject));
|
| ...
| }


Note that the 'sender' parameter is null. In a static method, I don't have
an instance, so I can't pass in 'this', and it seems silly for me to require
the caller to pass in it's 'this' reference...

I suppose I could pass in a reference to the static collection member that
contains the list of Fubar objects, but that seems pretty meaningless.

Is there a typical practice here? Is passing 'null' for sender OK in this
situation?

Thanks in advance.

-Scott
 
M

Michael C

news-server.san.rr.com said:
Is there a typical practice here? Is passing 'null' for sender OK in this
situation?

Makes sense to me. It fits that the sender is null because there is no
instance sending the event.

Michael
 
B

Bruce Wood

The "sender" just forms part of the contract between the event source
and the event handler. If the event source documents and defines the
event such as "sender" is null, then it's up to the handler to
understand that and not try to use it.

So, yes, that's the right way to do it. Just document that fact where
you declare the event (and, if you have a custom event delegate type,
where you declare the delegate).
 
M

Michael C

Bruce Wood said:
The "sender" just forms part of the contract between the event source
and the event handler. If the event source documents and defines the
event such as "sender" is null, then it's up to the handler to
understand that and not try to use it.

So you could really send anything through, eg clicking on a column in a grid
kinda control could use the column as the sender?

Michael
 
B

Bruce Wood

If you send an event from an object instance (like a column header in a
grid), then it's good form to pass the object that raised the event as
the "sender".

However, if you're in Scott's situation and you're raising the event
from a static method, and so you have no object instance handy, you
might just as well pass null, so long as the event handlers know to
expect that.
 
M

Michael C

Bruce Wood said:
If you send an event from an object instance (like a column header in a
grid), then it's good form to pass the object that raised the event as
the "sender".

However, if you're in Scott's situation and you're raising the event
from a static method, and so you have no object instance handy, you
might just as well pass null, so long as the event handlers know to
expect that.

The programmer using the event will work it out soon enough :)

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top