> [...]
> Here I define it just as onButtonAction.
>
> Which way is the way it is normally done?
Wow. That's a whole lotta code just to ask a relatively simple,
straightforward question. Next time, you might consider doing a little
editing.
In any case, the simple answer to your question is that there's not really
a "normal" way as you're asking it. It's not uncommon in a VS
Designer-created application to have the handlers in the same class where
the event is declared. For example, people writing event handlers for
events that are raised by the Form itself, handlers that go into the Form
class by default.
On the other hand, in those very same applications, it's also not uncommon
for an event handler to be in the Form class (since, again, that's where
they wind up by default) when it's handling an event on a different class
(for example, a Click event handler for a button control in the form
itself).
Now, that said, in spite of that my own personal preference is to
generally only subscribe to the event from outside the class, using a
handler in a different class. The reason being that from within the class
there's usually a better way to hook into the execution flow when
something that raises the event would happen (or at least there should
be). For example, if you're handling any event on the Form class, and
you've already got a sub-class of Form, there's a virtual function related
to the event (usually called "OnXXXX" where "XXXX" is the name of the
event) that you can override.
When I'm handling an event raised by a Form, and I'm sub-classing that
Form, I override the virtual function rather than subscribe to the event.
So architecturally, I view events as a way to expose things that happen to
the world outside a class. From within the class, I use whatever internal
mechanism is offered (or for my own classes, I provide it and use it).
However, lots of people use events by default, overriding a virtual
function only when they really have to, and frankly I don't see anything
really all that bad about doing it that way. Going through the event is
going to be less performant, but only nominally...it's not the kind of
thing that you'd ever notice in 99.9% of the situations.
So, what works best for you? What best fits your own mental model of how
the event paradigm behaves, and which way do you find the clearest to code
and to read? IMHO, either answer is "correct", and you should have a very
good reason for implementing something the other way (i.e. a reason much
better than "so-and-so does it that other way" :) ).
Pete
tshad - 10 Mar 2008 05:12 GMT
>> [...]
>> Here I define it just as onButtonAction.
[quoted text clipped - 4 lines]
> straightforward question. Next time, you might consider doing a little
> editing.
Actually, I usually do. I just wanted to show the exact same code show 2
different ways to get my point across of what I was asking.
> In any case, the simple answer to your question is that there's not really
> a "normal" way as you're asking it. It's not uncommon in a VS
[quoted text clipped - 37 lines]
> good reason for implementing something the other way (i.e. a reason much
> better than "so-and-so does it that other way" :) ).
I agree, but it is good to see if there is a standard way of doing (and why)
before making that decision as it may be easier to do or maintain for others
that have to use the code.
Thanks,
Tom
> Pete
Peter Duniho - 10 Mar 2008 05:47 GMT
>>> [...]
>>> Here I define it just as onButtonAction.
[quoted text clipped - 7 lines]
> Actually, I usually do. I just wanted to show the exact same code show 2
> different ways to get my point across of what I was asking.
Here's a more concise example:
class A
{
public event EventHandler EventA;
public A()
{
EventA += HandlerA;
}
private void HandlerA(object sender, EventArgs e) { }
}
class B
{
private A a = new A();
public B()
{
a.EventA += HandlerA;
}
private void HandlerA(object sender, EventArgs e) { }
}
Class A subscribes to its own event. Class B subscribes to class A's
event. This sample shows exactly the same two different ways you're
talking about, in much less code and without any extraneous stuff to
distract from the question (like calls to Console.WriteLine(), multiple
subscriptions to the event, etc.).
Pete