Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to use Delegates to Pass Procedure to another Procedure
Download Source Code :

In this sample application I will try to explain you how you can use delegates to pass procedure to another procedure. Here I have created this sample application using VS2005.

Visual Basic.Net (VB.NET) provides you flexibility that you can use delegate as you can use any other types. Using AddressOf operator you can return delegate object when you applied to a procedure name. This means that using AddressOf operator you can create a function delegate that points to the function specified by ProcedureName. Notice a function delegate can refer to both instance and the method if calling procedure is used as an instance method. Whenever function delegate is invoked it implicitly invokes the method. 


In this sample application I have created a simple windows form that calculates the salary. It takes simple input process input and display output. First of all create a Delegate function, it takes two parameters as an input:


Delegate Function CalculateSalary(ByVal x As Double, ByVal y As Double) As Double

Add two procedures, first procedure adds the allowances in salary and displays result on windows form:


Function AddAllouance(ByVal x As Double, ByVal y As Double) As Double

      Return x + y

End Function

Second procedure perform deduction if any:


Function LeaveDetuction(ByVal x As Double, ByVal y As Double) As Double

      Return x - y

End Function

Now create a procedure that takes a delegate as a parameter and compute the result by using invoke instance method:


Sub ComputeFinalSal(ByVal x As Double, ByVal op As CalculateSalary, ByVal y As Double)

      Dim result As Double

      result = op.Invoke(x, y)

      lblShowAmount.Text = result


End Sub

On the cmdCTS_Click call AddAllouance() procedure and on the cmdDeduct_Click event call LeaveDetuction procedure:


Private Sub cmdCTS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCTS.Click


      ComputeFinalSal(txtSal.Text, AddressOf AddAllouance, txtAllouance.Text)


End Sub


Private Sub cmdDeduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdDeduct.Click


      ComputeFinalSal(lblShowAmount.Text, AddressOf LeaveDetuction, txtDetuct.Text)


End Sub

Article Comments
Thank you,
Finally one person tat explains this in english and shows code that works :)

Posted on 3/9/2007 1:39:18 PM by Yaniv

Please clear what is achieved by this example ? We can do the same without using a delegate. What the sample asks for is simply calling AddAllowance and LeaveDeduction functions being called by two command buttons click event.

Please explain.

Posted on 4/11/2007 2:23:35 AM by Akash Lomas

Simple & Easy for the one Who wants to work with Delegates.

Posted on 6/20/2007 10:01:01 AM by Mahindra

Add Article Comment:
Name :
Email Address :
Comments :
<< How to get IP Address using VB.Net

Disclaimer - Privacy
© 2002-2018