F
FullBandwidth
I have been perusing various blogs and MSDN pages discussing the use
of event properties and the EventHandlerList class. I don't believe
there's anything special about the EventHandlerList class in this
context, in fact some articles from pre-2.0 suggest using any
collection class of your choice. So my questions focus more on the
syntax of event properties provided by the "event" keyword in C#.
(Disclaimer - I am a C++ programmer working on a C# development
project, so I tend to think in C++ and try and translate it in to C#.)
Are the "add" and "remove" properties of an event declaration defined
in a class somewhere, or are they part of the C# language definition?
Are there any other properties? Where are any of these documented?.
I also assume that the compelling reason to use event properties
defined this way is to allow event consumers to use += and -= to
subscribe and unsubscribe to events.
Every C# example I see has a very VB5-centric flavor to it -
specifically, each event must have a unique name defined in the source
code. For instance, here's the code segment from the MSDN article "How
to: Handle Multiple Events Using Event Properties"
// Define the delegate collection.
protected EventHandlerList listEventDelegates = new
EventHandlerList();
// Define a unique key for each event.
static readonly object mouseDownEventKey = new object();
static readonly object mouseUpEventKey = new object();
// Define the MouseDown event property.
public event MouseEventHandler MouseDown {
// Add the input delegate to the collection.
add { listEventDelegates.AddHandler(mouseDownEventKey, value); }
// Remove the input delegate from the collection.
remove { listEventDelegates.RemoveHandler(mouseDownEventKey,
value); }
}
// Define the MouseUp event property.
public event MouseEventHandler MouseUp {
// Add the input delegate to the collection.
add { listEventDelegates.AddHandler(mouseUpEventKey, value); }
// Remove the input delegate from the collection.
remove { listEventDelegates.RemoveHandler(mouseUpEventKey,
value); }
}
Any time I see code cut-and-pasted like this is a red flag that a
proper object-oriented decomposition of the problem hasn't been
performed. There are two events declared by name (MouseDown and
MouseUp) and yet the code that implements their properties "add" and
"remove" are nearly identical, except for the "key" argument (an
arbitrary static value, to provide a unique key for the
EventHandlerList class).
If the "key" argument could somehow be available to the add and remove
properties, this whole thing could be implemented as a class, and the
events, instead of having to be individually named at compile time,
could be part of a collection of objects of that class. However, the
"add" and "remove" properties seem to be a "hardcoded" notion in the
C# language, with the keyword "value" the only argument available to
the implementation of those properties (in this case, "value" is of
type Delegate). Am I misunderstanding this?
I have also read posts here that suggest "event" is syntactic sugar
such that the C# compiler will automatically generate the property
accessor functions, so that clients can use the += and -=.
If I wanted to ditch this notion that every event has to have a name
defined at compile time, I can think of how to implement such a class
hierarchy. However, I'm not sure if can override the += and -=
operators in my homegrown event properties implementation - would this
be possible?
Thanks
of event properties and the EventHandlerList class. I don't believe
there's anything special about the EventHandlerList class in this
context, in fact some articles from pre-2.0 suggest using any
collection class of your choice. So my questions focus more on the
syntax of event properties provided by the "event" keyword in C#.
(Disclaimer - I am a C++ programmer working on a C# development
project, so I tend to think in C++ and try and translate it in to C#.)
Are the "add" and "remove" properties of an event declaration defined
in a class somewhere, or are they part of the C# language definition?
Are there any other properties? Where are any of these documented?.
I also assume that the compelling reason to use event properties
defined this way is to allow event consumers to use += and -= to
subscribe and unsubscribe to events.
Every C# example I see has a very VB5-centric flavor to it -
specifically, each event must have a unique name defined in the source
code. For instance, here's the code segment from the MSDN article "How
to: Handle Multiple Events Using Event Properties"
// Define the delegate collection.
protected EventHandlerList listEventDelegates = new
EventHandlerList();
// Define a unique key for each event.
static readonly object mouseDownEventKey = new object();
static readonly object mouseUpEventKey = new object();
// Define the MouseDown event property.
public event MouseEventHandler MouseDown {
// Add the input delegate to the collection.
add { listEventDelegates.AddHandler(mouseDownEventKey, value); }
// Remove the input delegate from the collection.
remove { listEventDelegates.RemoveHandler(mouseDownEventKey,
value); }
}
// Define the MouseUp event property.
public event MouseEventHandler MouseUp {
// Add the input delegate to the collection.
add { listEventDelegates.AddHandler(mouseUpEventKey, value); }
// Remove the input delegate from the collection.
remove { listEventDelegates.RemoveHandler(mouseUpEventKey,
value); }
}
Any time I see code cut-and-pasted like this is a red flag that a
proper object-oriented decomposition of the problem hasn't been
performed. There are two events declared by name (MouseDown and
MouseUp) and yet the code that implements their properties "add" and
"remove" are nearly identical, except for the "key" argument (an
arbitrary static value, to provide a unique key for the
EventHandlerList class).
If the "key" argument could somehow be available to the add and remove
properties, this whole thing could be implemented as a class, and the
events, instead of having to be individually named at compile time,
could be part of a collection of objects of that class. However, the
"add" and "remove" properties seem to be a "hardcoded" notion in the
C# language, with the keyword "value" the only argument available to
the implementation of those properties (in this case, "value" is of
type Delegate). Am I misunderstanding this?
I have also read posts here that suggest "event" is syntactic sugar
such that the C# compiler will automatically generate the property
accessor functions, so that clients can use the += and -=.
If I wanted to ditch this notion that every event has to have a name
defined at compile time, I can think of how to implement such a class
hierarchy. However, I'm not sure if can override the += and -=
operators in my homegrown event properties implementation - would this
be possible?
Thanks