Visual Basic Func Delegate

In visual basic, Func is a built-in generic delegate and it is useful to hold the reference of one or more methods which is having same method signature without declaring any custom delegate object.

 

In the previous chapters, we learned about delegates and that will be used like as shown following to hold the reference of methods which is having the same signature.

 

Module Module1

    ' Declare Delegate

    Public Delegate Function SampleDelegate(ByVal a As Integer, ByVal b As Integer) As Integer

    Sub Main(ByVal args As String())

        Dim result As Integer = 0

        Dim dlgt As SampleDelegate = AddressOf Add

        result = dlgt(10, 90)

        Console.WriteLine("Add Result: {0}", result)

        dlgt = AddressOf Subtract

        result = dlgt(10, 90)

        Console.WriteLine("Subtract Result: {0}", result)

        Console.ReadLine()

    End Sub

    Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer

        Return a + b

    End Function

    Public Function Subtract(ByVal x As Integer, ByVal y As Integer) As Integer

        Return x - y

    End Function

End Module

If you observe the above example, we created a delegate object called “SampleDelegate” to hold the reference of Add & Subtract methods.

 

When we execute the above example, we will get the result like as shown below.

 

Add Result: 100

Subtract Result: -80

To avoid the declaration of custom delegate object (SampleDelegate) like as we defined in the above example, the generic built-in delegates such as Func, Action, Predicate has been introduced in latest versions.

 

In latest versions, the Func delegate will available automatically with System namespace and it will accept zero or more (16) input parameters and one output parameter.

Visual Basic Func Syntax

Following is the syntax of declaring a Func delegate with one input parameter and one output parameter in visual basic.

 

Public Delegate Function Func(Of In T, Out TResult)(ByVal arg As T) As TResult

Here, the Out parameter in the bracket ( ) will be considered as output parameter (return type) and remaining all are considered as input parameters. 

 

ParameterDescription
TResult It represent the type of the return value of the method that the delegate encapsulates.
T It represent the type of the parameter of the method that the delegate encapsulates.

In case, if we want to create a Func delegate with two input parameters and return type (output parameter) that would be like as shown below.

 

Public Delegate Function Func(Of In T1In T2, Out TResult)(ByVal arg As T1ByVal arg As T2As TResult

Like this a Func delegate can include 0 to 16 input parameters of different types and one output parameter for the result. 

Visual Basic Func Example

Following is the example of defining the Func delegate to hold the reference of one or more methods which is having same method signature.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim result As Integer = 0

        Dim dlgt As Func(Of Integer, Integer, Integer) = AddressOf Add

        result = dlgt(10, 90)

        Console.WriteLine("Add Result: {0}", result)

        dlgt = AddressOf Subtract

        result = dlgt(10, 90)

        Console.WriteLine("Subtract Result: {0}", result)

        Console.ReadLine()

    End Sub

    Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer

        Return a + b

    End Function

    Public Function Subtract(ByVal x As Integer, ByVal y As Integer) As Integer

        Return x - y

    End Function

End Module

If you observe the above example, we created a Func delegate object (dlgt) with two input parameters (int) and one output parameter (int) to return int value.

 

Here, the declaration of Func(Of Integer, Integer, Integer> dlgt is same as SampleDelegate object in the previous example.

 

When we execute the above example, we will get the result like as shown below.

 

Add Result: 100

Subtract Result: -80

Every time while creating the Func delegate we must need to remember that we can include 0 to 16 input parameters of different types and that is optional, but we must need to include one output parameter for the return type.

 

Following is the example of creating a Func delegate with zero (0) input parameters and one output parameter.

 

Dim dlgt As Func(Of Integer)

Visual Basic Func with Anonymous Method

In visual basic, we can assign the anonymous method directly to the Func delegate by using delegate keyword like as shown below.

 

Sub Main(ByVal args As String())

    Dim dlgt As Func(Of Integer, Integer, Integer) = Function(x As Integer, y As Integer) x + y

    Dim i As Integer = dlgt(10, 90)

    Console.WriteLine("Result: {0}", i) ' Result: 100

End Sub

If you observe the above code, we assigned an anonymous method directly to Func delegate object (dlgt) using the delegate keyword.

Visual Basic Func with Lambda Expressions

In visual basic, we can also use Func delegate with lambda expressions. The lambda expressions are the shorthand way for declaring the anonymous method.

 

Sub Main(ByVal args As String())

    Dim dlgt As Func(Of IntegerIntegerInteger) = Function(x, y) x + y

    Dim i As Integer = dlgt(10, 90)

    Console.WriteLine("Result: {0}", i) ' Result: 100

End Sub

Visual Basic Func Overview

Following are the important points which we need to remember about Func delegate in visual basic.

 

  • In visual basic, Func is a built-in generic delegate and it is useful to hold the reference of one or more methods which is having same method signature.
  • Func delegate is introduced in latest versions of visual basic and it is available with System namespace.
  • While creating a Func delegate, we can include 0 to 16 input parameters of different types and one output parameter for the result.
  • While creating a Func delegate, specifying input parameters is optional, but we must need to include one output parameter for the return type.
  • We can use Func delegate in anonymous methods and Lambda expressions.

Topics Covered