System.EventArgs

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Am I right in thinking that the e as System.EventArgs provided in most windows events never contains any useful information? Classes that derive from it, like System.Windows.Forms.MouseEventArgs, of course do provide useful information, but this seems to provide nothing.
 
System.EventArgs doesn't contain any useful information - it is designed for the situation where the event is enough information in itself. i.e. A button click event - the fact the event has fired indicates the button was clicked; there is no further information that needs to be supplied, therefore System.EventArgs suffices.
 
PlausiblyDamp said:
System.EventArgs doesn't contain any useful information - it is designed for the situation where the event is enough information in itself. i.e. A button click event - the fact the event has fired indicates the button was clicked; there is no further information that needs to be supplied, therefore System.EventArgs suffices.
I wonder why it's included as a parameter in the events then.
 
There is a standard in .Net where every event has two parameters: a sender declared as an object and arguments declared as EventArgs or a deriving class. If you look at EventArgs in the .Net Reflector you will see that there really is nothing to it. There is a point to it, however.

First of all, it is simpler when all events follow the convention. Secondly, it allows you to use contra-variance. A single event handler can take advantage of contra-variance and handle any event that conforms to this standard, but only because every event passes an EventArgs or derived class.
Visual Basic:
Public Sub UniversalEventHandler(sender As Object, e As EventArgs)
    MessageBox.Show("Message recieved from " & sender.ToString() & " with " & e.GetType().ToString() & ".")
End Sub
 
Public Sub Demonstrate()
    'See how having an EventArgs makes events more flexible, even though 
    'The EventArgs might not carry any data?
 
    AddHandler SomeButton.Click, AddressOf UniversalEventHandler
    AddHandler SomePanel.MouseMove, AddressOf UniversalEventHandler
    AddHandler SomeTreeView.BeforeLabelEdit, AddressOf UniversalEventHandler
End Sub

This is seldom useful, but it is really beside the point anyways. The big picture is that all events should follow a standard.
 
Back
Top