Delegates


Adelegate is an object 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 class and specifies the parameters and return type of the method to which the delegate will refer.

The following code uses a Delegate statement to declare the StringDisplayerType to be a delegate to a subroutine that takes a string as a parameter. Next, the code declares the variable m_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.

  ' 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 m_DisplayStringRoutine As StringDisplayerType ' Assign the variable to a subroutine. m_DisplayStringRoutine = AddressOf ShowStringInOutputWindow ' Invoke the delegate's subroutine. m_DisplayStringRoutine("Hello world") 

The delegate in the previous 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.

The following code uses delegates for both of these functions. First, it 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.

  Dim emp As New Employee("Rod", "Stephens") Private Delegate Function NumEmployeesDelegate() As Integer Private Sub btnShared_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnShared.Click     Dim show_num As NumEmployeesDelegate     show_num = AddressOf Employee.GetNumEmployees     MessageBox.Show(show_num().ToString) End Sub Private Delegate Function GetNameDelegate() As String Private Sub btnInstance_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnInstance.Click     Dim show_name As GetNameDelegate     show_name = AddressOf emp.ToString     MessageBox.Show(show_name()) End Sub 

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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net