views:

122

answers:

3

I know they have something to do with delegates. I've tried but I still don't comprehend how to use those either. I know a little about event handlers but what I really want to know is how I can use plain old eventargs that are part of most methods. example below

void Page_Load(object sender, EventArgs e)
{
myText.Value = "Hello World!";
}

Thanks for your time & consideration, I am just trying to be the best coder I can be.

Mike

+2  A: 

The base EventArgs class is an emtpy class that you can't use directly.

However, there are many derived EventArgs classes that provide data about an event.

For example, the KeyPress event gives a KeyPressEventArgs which tells you which key was pressed.

If you get an event handler where the e parameter is an inherited EventArgs, you can use the properties on the inherited EventArgs object to find out more about the event.

SLaks
I agree with everything you said except not being able to use `EventArgs` directly - what prevents someone from using `EventArgs` directly?
Andrew Hare
There is nothing you can do with it.
SLaks
http://msdn.microsoft.com/en-us/library/system.eventargs.aspx
IrishChieftain
@SLaks - I still don't completely understand - one of the main usages of the type is its `Empty` property which is very useful when you need to raise a method without any `EventArgs`.
Andrew Hare
I meant that there is nothing an event handler can do with an `EventArgs` instance. (Except cast it)
SLaks
+1  A: 

Plain old EventArgs are not really that useful, however types that derive from EventArgs are much more useful since you can define other members to carry strongly-typed data.

A good example of this is the GridViewEditEventArgs that are passed by a GridView inherit from EventArgs but extend it to provide NewEditIndex and Cancel properties.

Andrew Hare
+5  A: 

EventArgs classes are used as data carriers when raising events. They typically contain information that is related to the event being raised.

Many events use the EventArgs class, which contains no particular information. This class also serves as the base class for all other EventArgs classes. One example of a more specific EventArgs class is the TreeNodeEventArgs class, that is used by a number of events, and that contains information about which TreeNode that the event is related to.

In some cases, EventArgs classes can be designed so that they allow event handlers to communicate data back to the source that raised the event, an example of that is the CancelEventArgs class.

Fredrik Mörk