rbulph Posted September 29, 2006 Posted September 29, 2006 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. Quote
Administrators PlausiblyDamp Posted September 29, 2006 Administrators Posted September 29, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rbulph Posted September 29, 2006 Author Posted September 29, 2006 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. Quote
Gill Bates Posted September 29, 2006 Posted September 29, 2006 Just so it can follow the conventions that the framework uses. Quote
Leaders snarfblam Posted October 1, 2006 Leaders Posted October 1, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.