In This Chapter
Using the EventHandler Delegate
Reviewing Delegate Members
Passing Delegates as Arguments
Using Delegates Across Project Boundaries
Visual Basic 6 provided an opportunity for us to become familiar with events as a dynamic aspect of Windows programming. From Chapter 8, you know that an event is an occurrence in your program, and an event handler is a procedure defined to respond to that occurrence.
To review, the event-handling mechanism works because procedures are effectively addresses. If we know the arguments that are passed to a procedure and have a procedure's address, we can invoke a procedure because this is how procedure invocation works internally. VB6 allowed us to pass the address of a procedure to Windows for API calls that needed a callback address, but didn't support callbacks within VB6 itself. Visual Basic .NET supports procedural types through the Delegate class. Delegates maintain the addresses of procedures used as callback procedures. When referring to a procedural type, think of a variable declaration whose type happens to be the signature of a procedure. Procedural type is the generic term that has been in existence in other languages for years ; in Visual Basic .NET, procedural types are specifically referred to as delegates.
Because delegates are classes in Visual Basic .NET, we have extended capabilities beyond one instance of a Delegateprocedural typecontaining a single address of one procedure. Delegates are implemented to support a list of addresses referred to as an invocation list. Delegates that contain multiple procedure addresses are referred to as multicast delegates. Multicast delegates support a single event having multiple respondents.
VB6 required a control array if you wanted one event handler to handle events for multiple controls. Visual Basic .NET introduces the Delegate class to keep track of event handlers. Visual Basic .NET supports multiple event handler respondents for a single control event and supports multiple controls being associated with a single event handler.
In Chapter 9, you will learn all about defining, declaring, and invoking delegates. Additionally, I will demonstrate how delegates can be used as procedure arguments to support dynamic behavior. We will begin coverage of delegates in this chapter by looking at one of the most common pre-existing delegates, EventHandler.