Featured post
c# - Why can I check some event handlers for null, some not? -
i have ugly piece of code adds event handlers. problem is, if code called multiple times, event handlers called multiple times.
to solve problem, remove event handler first , add it.
now i've seen following behaviour:
some event handlers can checked like:
if (object.event == null) { // // code // }
others of form
if (object.object.event == null) { // // code // }
i message 'object.object.event' may occur left of -= or +=. (since i'm using german version of visual studio, don't know correct translation english).
i have no idea why behaviour looks inconsequent grateful information on this.
to more specific: it's user control.
if (mycontrol.event == null) { // // works // } if (mycontrol.treeview.nodemouseclick == null) { // // doesn't work // }
slaks correct, , has linked excellent resources. here's relevant quote chris burrows' blog article:
let me take quick detour here , explain how binding of += works in c#. there 2 possibilities:
- either there actual + operator, such ints, , x += y binds “x = x + y” except x evaluated once. compound assignment operator; or
- the thing on left hand side event, , x.e += y binds “x.add_e(y)”. event accessor operator, , in fact way bind event accessor.
so have in snippet above? well, bit of detail need decide following rule field-like events in c#: outside of class or struct defines field-like event e, binding name e resolves event itself, on legal operation calling accessor; inside class or struct defines field-like event e, binding name e resolves private delegate field.
in case, when resolving mycontrol.event
, you're inside mycontrol
class, don't see event object; instead see actual delegate object, can compare null. when resolving mycontrol.treeview.nodemouseclick
, you're outside treeview
class, can't access actual delegate object; event object, cannot compared null.
if understand correctly, of wouldn't anyway, since presumably after check null, you're going try fire treeview
's event it, can't do.
depending on you're trying do, subclass treeview
, add internal
method call protected treeview.onnodemouseclick
method fire event.
- Get link
- X
- Other Apps
Comments
Post a Comment