Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse delegates to pass a method's address to another method in Visual Basic .NET
DescriptionThis example shows how to use delegates to pass a method's address to another method in Visual Basic .NET.
Keywordsdelegate, plot, graph, plot function, graph function, Visual Basic .NET, VB.NET
CategoriesVB.NET, Software Engineering
 

The example Plot the equation of a function of two variables in Visual Basic .NET shows how to draw a graph of a function even if you don't really understand its shape ahead of time. That example's PlotFunction method invokes the function F1 to get values for the function to plot.

The PlotFunction method would be a lot more useful if it could plot lots of different functions. This example shows how you can use delegates to do just that.

A delegate is basically a pointer to a method. You can make a delegate type that indicates a specific type of method, such as one that takes two parameters and returns void, or one that takes no parameters and returns an int.

After you define a delegate type, you can create variables of that type, assign them to point to a particular method, and invoke them.

This example uses the following code to define a delegate type named FofXY. This represents a method that takes two float parameters and returns a float value.

 
' Define a delegate type named FofXY.
Private Delegate Function FofXY(ByVal x As Single, ByVal y _
    As Single) As Single
 
The program's DrawGraph method prepares a Bitmap to hold the graph and then calls PlotFunction to do the actual plotting. The following code shows DrawGraph's declaration. Notice the parameter of type FofXY.
 
' Draw the indicated function.
Private Sub DrawGraph(ByVal func As FofXY)
    ...
End Sub
 
The DrawGraph method uses the following code to invoke PlotFunction. Notice how it passes the delegate variable func into PlotFunction.
 
PlotFunction(gr, func, -8, -8, 8, 8, dx, dy)
 
PlotFunction's declaration includes a delegate parameter much as DrawGraph's declaration does.
 
' Plot a function.
Private Sub PlotFunction(ByVal gr As Graphics, ByVal func _
    As FofXY, _
 ByVal xmin As Single, ByVal ymin As Single, ByVal xmax As _
     Single, ByVal ymax As Single, _
 ByVal dx As Single, ByVal dy As Single)
    ...
End Sub
 
PlotFunction uses code similar to the following to invoke the delegate that it is passed.
 
Dim last_y As Single = func(x, ymin)
 
When you click the program's radio buttons, their event handlers invoke DrawGraph, passing it the appropriate function. For example, when you click the last radio button, the following code calls DrawGraph, passing it the method F2 to generate the result shown at the beginning of this entry.
 
Private Sub radF2_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    radF2.Click
    DrawGraph(AddressOf F2)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated