Click here to Skip to main content
15,867,765 members
Articles / Programming Languages / Visual Basic

Ways of Delegation in .NET (C# and VB)

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
15 Oct 2012CPOL2 min read 12.7K   10   2
In this post I am going to discus about the delegate uses in C# and VB.NET.

Introduction: Straightly speaking delegation is a process of pointing a function. So a delegate type variable is a pointer of a function. In this post I am going to discus about the delegate uses in C# and VB.NET. Declaring an using a delegate is simple with four steps. If  I give an analogy with a normal variable type like integer or string or float then things will look more simple. The types integer and string are built-in in the system so need not declare their types. But for the delegate type we will have to declare that type with delegate keyword and the function signature (the function it will point to).

Step 1: Declaring a delegate type

Blue: C# code

C#
delegate int MyDel(string input); 

and Green: VB code

VB
Delegate Function MyDel(str As String) As Integer

The this declaration means that MyDel type variable can only point to the functions that returns integer and takes a string as input parameter.

Step 2: Define a variable

Now  time to define a variable for that delegate type. In C#

C#
MyDel func;

In VB.NET

VB
Dim func as MyDel

This is same as defining a variable x for integer type.

C#
int x;

Now what happens when you create a variable of a delegate type? It simply creates a pointer type variable that can point to a function. In pure C/C++ they are called function pointer.

Step 3: Assign a function to the variable

At this step we simply assign a function to this variable. This is as like assign a value of 5 to our integer type variable x.

C#
x = 5;

There are 3 ways to hook up a function with a variable in C# – using the delegate keyword, using lambda expression and the ordinary way. We will point a function that takes a string as input and returns the length of the string in C#.

VB
// Assign our variable a new function (created via delegate)
func = delegate(string str) { return str.Length; };

or

C#
// Assign our variable a new function (created via lambda expressions)
func = (str => str.Length);

or

C#
// If the function is declated somewhere else as FunctionFoo
func = FunctionFoo;
// FunctionFoo declared somewhere else as
private int FunctionFoo(string str)
{
     return str.Lenght;
}

Now in VB.NET there are only two ways – lamdba expression and the ordinary way. Use the lambda expression way as bellow

VB
' When the function returns
func = Function(str) str.Length
func = Function(str)
    Return str.Length()
End 

' When the function doesn't return (not appropriate for our delegate type)
func = Sub(str) Console.WriteLine(str.Length)
func = Sub(str)
    Console.WriteLine(str.Length)
End Sub

or the ordinary way

VB
// If the function is declated somewhere else as FunctionFoo
f = AddressOf FunctionFoo
// FunctionFoo declared somewhere else as
Function FunctionFoo(str As String) As String
    Return str.Length
End Function

Step 4: Calling the functions using delegate variable
Now is the time to call the function. To do this do this in C#

C#
func("My Input string");

and in VB.NET

VB
func("My Input string")

or

VB
func.Invoke("My Input string")

Conclusion: This is very basic but yet very important topic specially when you want to route some functionality among different objects. Hope this will help you in your project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Nick Hounsome15-Oct-12 21:38
Nick Hounsome15-Oct-12 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.