Classes - Events
What is an Event?
An Event is an occurance that gets triggered within a class.
Most often, properties will have Events that get triggered when the value changes.
Example
The following demonstrates how to create an Event:
Fiddle: Live Demo
Parameters
Events may contain optional parameters.
The parameters that are present in an Event makes up the Event's signature.
If an event does not have a need for any parameters, then the best practices is to create two parameters:
- ByVal sender As Object
- ByVal e As EventArgs
The sender will represent the Class that is invoking the Event. The e is special EventArgs, which if there none, then EventArgs.Empty can be passed as the argument.
Chains
When a Class' Event is created outside the class, it creates a method that is bound to Event in some manner or another, typically by using the Handles keyword or the AddHandler statement.
Events can be chained together to execute the same code, so long as they share the same event signature. This is why it is best practices to include the sender and e parameters.
The following demonstrates a chained event signature:
Fiddle: Live Demo
Remarks
Many Classes will require that an Event be invoked when a Property changes. Visual Basic .NET has a built-in event that automatically does this. For more information on PropertyChanged events, visit this link.