Anout raise events

T

Tony Johansson

Hi!

Here I have some text that I dont fully understand. It says.
"Which of the following elements are NOT required if you want a custom class
to raise an event.

1 A class that holds event data derived from System.EventArgs
2 A delegete for the event
3 A method to add an eventhandler
4 A class that raises the event.

The answer is 3:
You do not need a method to add an event handler that functionalitty is
provided by the .NET Framework.

When I first read this I thould that the meant the event hander that is
acting when the event is raised but that is
probable wrong.

So what do they mean with this row A method to add an eventhandler

//Tony
 
V

vanderghast

They probably mean a method to handle the event, to consume it, to react to
it. Since the question is about raising the event, not handling it, you
don't have to have a method to handle it.

Vanderghast, Access MVP
 
P

Peter Duniho

Tony said:
Hi!

Here I have some text that I dont fully understand. It says.
"Which of the following elements are NOT required if you want a custom class
to raise an event.

1 A class that holds event data derived from System.EventArgs
2 A delegete for the event
3 A method to add an eventhandler
4 A class that raises the event.

As usual, your source is woefully inadequate and embarrassingly flawed.
The answer is 3:
You do not need a method to add an event handler that functionalitty is
provided by the .NET Framework.

First, the event's add and remove method are provided by the C#
compiler, not the .NET Framework.

Second, you can have a custom class with a custom event that does not
require a class deriving from System.EventArgs. Your custom event can
simply use System.EventArgs, or you can define a completely different
event signature that doesn't involve the use of System.EventArgs in any way.

It is true that there needs to be _some_ delegate type; after all, you
can't declare an event without a delegate type! And of course, in C#
everything has to be in a class, so of course the code that raises the
event also needs to be in a class. Duh.
When I first read this I thould that the meant the event hander that is
acting when the event is raised but that is
probable wrong.

So what do they mean with this row A method to add an eventhandler

The method they are talking about is the add method in the event.
Similar to automatic properties, when you declare an event like this:

public event EventHandler MyEvent;

…the compiler automatically provides the _required_ implementation for you:

private EventHandler MyEventField;
public event EventHandler MyEvent
{
add { MyEventField += value; }
remove { MyEventField -= value; }
}

All events have both an add and a remove method. It's just that most
people never see them because they are using the automatic event
implementation syntax.

You are, of course, free to provide any explicit implementation for the
event that you desire. But it's not required. Hence the choice of
answer #3 as the "most correct" answer for the question.

Noting of course that answer #1 is technically also a correct answer.
That's how much the book you're using sucks.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top