DELEGATES

A delegate is a type that refers to a subroutine, function, or other method. The method can be an instance method provided by an object, a class’s shared method, or a method defined in a code module. A delegate variable acts as a pointer to a subroutine or function. Delegate variables are sometimes called type-safe function pointers.

The Delegate keyword defines a delegate type and specifies the parameters and return type of the method to which the delegate will refer.

The following code demonstrates a delegate:

' Define a StringDisplayerType delegate to be a pointer to a subroutine
' that has a string parameter.
Private Delegate Sub StringDisplayerType(ByVal str As String)
 
' Declare a StringDisplayerType variable.
Dim DisplayStringRoutine As StringDisplayerType
 
' Assign the variable to a subroutine.
DisplayStringRoutine = AddressOf ShowStringInOutputWindow
 
' Invoke the delegate's subroutine.
DisplayStringRoutine("Hello world")

The code uses a Delegate statement to declare the StringDisplayerType to be a reference to a subroutine that takes a string as a parameter. Next, the code declares the variable DisplayStringRoutine to be of this type. This variable can hold a reference to a subroutine that takes a string parameter. The code then sets the variable equal to the ShowStringInOutputWindow subroutine. Finally, the code invokes the delegate’s subroutine, passing it a string.

The delegate in the preceding example holds a reference to a subroutine defined in a code module. A delegate can also hold the address of a class’s shared method or an instance method. For example, suppose the Employee class defines the shared function GetNumEmployees that returns the number of employees loaded. Suppose that it also defines the instance function ToString that returns an Employee object’s first and last names.

Example program UseDelegates, which is available for download on the book’s website, uses the following code to demonstrate delegates for both of these functions:

Dim emp As New Employee("Rod", "Stephens")
 
' Use a delegate pointing to a shared class method.
Private Delegate Function NumEmployeesDelegate() As Integer
 
Private Sub btnShared_Click() Handles btnShared.Click
    Dim show_num As NumEmployeesDelegate
    show_num = AddressOf Employee.GetNumEmployees
    MessageBox.Show(show_num().ToString, "# Employees")
End Sub
 
' Use a delegate pointing to a class instance method.
Private Delegate Function GetNameDelegate() As String
Private Sub btnInstance_Click() Handles btnInstance.Click
    Dim show_name As GetNameDelegate
    show_name = AddressOf emp.ToString
    MessageBox.Show(show_name(), "Name")
End Sub

First, the program declares and initializes an Employee object named emp. It then defines a delegate named NumEmployeesDelegate, which is a pointer to a function that returns an integer. The btnShared_Click event handler declares a variable of this type, sets it to the address of the Employee class’s shared GetNumEmployees function, and calls the function. Then the code defines a delegate named GetNameDelegate, which is a pointer to a function that returns a string. The btnInstance_Click event handler declares a variable of this type, sets it to the address of the emp object’s ToString function, and then calls the function.

These examples are somewhat contrived because the code could easily invoke the subroutines and functions directly without delegates, but they show how a program can save a delegate pointing to a subroutine or function and then call it later. A real application might set the delegate variable’s value and only use it much later.

A particular delegate variable could hold references to different methods, depending on the program’s situation. For example, different subroutines might generate output on a form, on the printer, or into a bitmap file. The program could set a delegate variable to any of these routines. Later, the program could invoke the variable’s routine without needing to know which routine will actually execute.

Another useful technique is to pass a delegate variable into a subroutine or function. For example, suppose that you are writing a subroutine that sorts an array of Customer objects. This routine could take as a parameter a delegate variable that references the function to use when comparing the objects in the array. By passing different functions into the routine, you could make it sort customers by company name, contact name, customer ID, total past sales, or anything else you can imagine.

Delegates are particularly confusing to many programmers, but understanding them is worth a little extra effort. They can add an extra dimension to your programming by essentially allowing you to manipulate subroutines and functions as if they were data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.241.69