Visual Basic Action Delegate

In visual basic, Action is a built-in generic delegate same as Func delegate to hold the reference of one or more methods but the only difference is the Action delegate will not return any value.

 

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

 

Module Module1

    ' Declare Delegate

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

    Sub Main(ByVal args As String())

        Dim dlgt As SampleDelegate = AddressOf Add

        dlgt(10, 90)

        dlgt = AddressOf Subtract

        dlgt(10, 90)

        Console.ReadLine()

    End Sub

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

        Console.WriteLine("Add Result: {0}", a + b)

    End Sub

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

        Console.WriteLine("Subtract Result: {0}", x - y)

    End Sub

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 have been introduced in the latest versions.

 

In latest versions, the Action delegate will available automatically with System namespace and it will accept zero or more (16) input parameters and does not return a value.

Visual Basic Action Delegate Syntax

Following is the syntax of declaring an Action delegate with one input parameter in visual basic.

 

Public Delegate Sub Action(Of In T)(ByVal arg As T)

Here, ByVal parameters in the brackets ( ) will be considered as input parameters and Sub is the return type.

 

In case, if we want to create an Action delegate with two input parameters that would be like as shown below.

 

Public Delegate Sub Action(Of In T1In T2)(ByVal arg As T1ByVal arg As T2)

Like this, an Action delegate can include 0 to 16 input parameters of different types based on our requirements. 

Visual Basic Action Delegate Example

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

 

Module Module1

    Sub Main(ByVal args As String())

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

        dlgt(10, 90)

        dlgt = AddressOf Subtract

        dlgt(10, 90)

        Console.ReadLine()

    End Sub

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

        Console.WriteLine("Add Result: {0}", a + b)

    End Sub

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

        Console.WriteLine("Subtract Result: {0}", x - y)

    End Sub

End Module

If you observe the above example, we created an Action delegate object (dlgt) with two input parameters (int) and assigned the methods directly to the delegate object.

 

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

 

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

 

Add Result: 100

Subtract Result: -80

Every time while creating the Action delegate we must need to remember that we can include 0 to 16 input parameters of different types and that is optional.

 

In visual basic, we can also initialize an Action delegate using new keyword with required input parameters like as shown below.

 

Dim dlgt As Action(Of Integer, Integer) = New Action(Of IntegerInteger)(Add)

Visual Basic Acton Delegate with Lambda Expressions

In c#, we can also use Action delegate with lambda expressions. The lambda expressions are the shorthand way to declare the anonymous method.

 

Module Module1

    Sub Main(ByVal args AsString())

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

                                                   Console.WriteLine("Result: {0}", x + y)

                                                  End Function

        dlgt(10, 90) ' Result: 100

        Console.ReadLine()

    End Sub

End Module

Visual Basic Action Delegate Overview

The following are the important points which we need to remember about Action delegate in visual basic.

 

  • In c#, Action is a built-in generic delegate same like Func delegate but the only difference is the Action delegate will not return any value.
  • While creating an Action delegate, we can include 0 to 16 input parameters of different types.
  • We can use Action delegate in anonymous methods and lambda expressions.