0% found this document useful (0 votes)
74 views2 pages

Delegates Example in VB - NET and C#

Delegates are similar to function pointers in other languages, and used to pass a function as an argument to another function.

Uploaded by

horizonsahib1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views2 pages

Delegates Example in VB - NET and C#

Delegates are similar to function pointers in other languages, and used to pass a function as an argument to another function.

Uploaded by

horizonsahib1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Public Class Form1

'Declaring Delegate
Delegate Function MyMath(ByVal x As Integer, ByVal y As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim dele As MyMath 'Create Delegate Type Object

Dim sum, diff, div, mul As Integer

'Passing Reference of Function to Delegate


dele = New MyMath(AddressOf Add)
sum = dele(10, 5) 'Calling Function using Delegate Object

dele = New MyMath(AddressOf Subtract)


diff = dele(10, 5)

dele = New MyMath(AddressOf Divide)


div = dele(10, 5)

dele = New MyMath(AddressOf Multiply)


mul = dele(10, 5)

Label1.Text = sum & vbCrLf & diff & vbCrLf & div & vbCrLf & mul

End Sub
Private Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
Private Function Subtract(ByVal x As Integer, ByVal y As Integer) As Integer
Return x - y
End Function
Private Function Divide(ByVal x As Integer, ByVal y As Integer) As Integer
Return x / y
End Function
Private Function Multiply(ByVal x As Integer, ByVal y As Integer) As Integer
Return x * y
End Function
End Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
//Delcaring Delegate
public delegate int MyMath(int x, int y);

public static int add(int x, int y)


{
return x + y;
}
public static int subtract(int x, int y)
{
return x - y;
}
public static int divide(int x, int y)
{
return Convert.ToInt32(x / y);
}
public static int multiply(int x, int y)
{
return x * y;
}

static void Main(string[] args)


{
MyMath x; //Instantiating Delegate (Creating Object)

int sum, diff, div, mul;

x = add; //giving method's reference to delegate


sum=x(10, 5);

x = subtract;
diff=x(10,5);
x = divide;
div = x(10, 5);

x = multiply;
mul = x(10, 5);

Console.WriteLine("\n\t\tDelegate Example : Kshitij Divakar ");


Console.WriteLine("\n\t\tSum = {0}", sum);
Console.WriteLine("\n\t\tDifference = {0}", diff);
Console.WriteLine("\n\t\tDivision = {0}", div);
Console.WriteLine("\n\t\tMultiplication = {0}", mul);

Console.ReadKey();
}

}
}
--End--

You might also like