Check Event Already Subscribed

  • Thread starter Thread starter Bijoy Thulasidharan
  • Start date Start date
B

Bijoy Thulasidharan

Hi ,

Hope you should be able to help us out with the below issue

Before subscribing to a delegate like this

cell.OnGridCellSelected += new
System.EventHandler(_OnGridCellSelected);

we would like to check whether it was already subscribed to .

Ofcourse the compiler doesnt allow checks like these

if (!cell.OnGridCellSelected=null) cell.OnGridCellSelected
+= new System.EventHandler(_OnGridCellSelected);

what we are instead doing is

cell.OnGridCellSelected -= new
System.EventHandler(_OnGridCellSelected);

cell.OnGridCellSelected += new
System.EventHandler(_OnGridCellSelected);

which we arent sure whether is exactly a good practise

Any sample illustrations or alternative suggestions would be highly regarded

Kind Regard s

Bijoy
 
Hi,

If you are the only one who subscribes to this event, you could introduce a
hashtable with a reference to the cell as the key and a boolean flag as the
value. When the boolean flag is set for a cell, it would mean that the event
handler has been already installed.

But even better, you should re-think the application architecture to have
only one point at which event handlers are installed, so you will always be
in control of whether a certain event handler is installed or not.
 
Back
Top