Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
  • Administrators
Posted
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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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' date=' therefore System.EventArgs suffices.[/quote']

I wonder why it's included as a parameter in the events then.

  • Leaders
Posted

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.

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...