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 8 6031
"news-server.san.rr.com" <sc*************@NO-SPAM.hotmail.com> wrote in
message news:t_*******************@tornado.socal.rr.com... 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
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).
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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
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.
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.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Liang |
last post by:
Hello,
Could anyone tell me how to define static event handle field by using
CodeDom?
Also is there a bug in CodeDelegateCreateExpression which requires its
targetobject parameter is not null...
|
by: Jakub Otahal |
last post by:
Hi all,
I have problem........ I have array of buttons and I want to call one
common event service. But I need to know the index of clicked button -- how
can I do that?
thanks
jakub
|
by: Mike P |
last post by:
When I raise an event in my ASP.NET (such as OnServerValidate), how do I
get details on the Sender in the called procedure e.g.
public void Check_Log_Details(object sender,...
|
by: CodeBlue |
last post by:
Hi,
(these are sample classes to illustrate)
I have 3 classes:
public class A
{
|
by: Dave Booker |
last post by:
What is the right way to declare a static event handler?
I have a:
delegate void TestEvent(string ID, ref string payload);
namespace TestNS
{
public class TestFrm :...
|
by: =?Utf-8?B?TkVXMi5ORVQ=?= |
last post by:
I have a static event declared in a C++ ref class, that can then be handled
in a VB app. I'm trying to expose the static event through the interface
that the C++ ref class implements so the VB app...
|
by: Alan Woodland |
last post by:
Hi,
I'm fairly sure this is undefined behaviour, despite the fact that
it compiles and 'runs' (prints "this doesn't exist") on all my platforms:
#include <iostream>
class foo {
public:...
|
by: sejal17 |
last post by:
hello all,
i am developing asp.net application that is running on local machine but when i am uploading this application on web server it is display error when i am clicking on edit/delete in grid....
|
by: TDHall |
last post by:
When a mouse click event is triggered the code goes to an event handler that I set up:
(private void listBox2_MouseClick(object sender, MouseEventArgs e)).
Viewing the "Locals" in debug shows...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
| | |