Visual Basic Anonymous Methods

In visual basic, anonymous methods are the methods without a name. Generally, the anonymous methods can be defined by using delegate keyword and can be assigned to the variable of the delegate type.

 

Following is the sample way of creating anonymous methods in visual basic using delegate keyword.

 

// Create a delegate

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

// Instantiate the delegate using an anonymous method

Dim ops As MathOps = Sub(ByVal x As Integer, ByVal y As Integer)

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

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

                     End Sub

If you observe the above code, we created anonymous method and passing it as a delegate parameter and assigned to a variable of the delegate type.

 

In visual basic, anonymous methods are useful to reduce the coding overhead in instantiating delegates because we don’t have to create a separate method.

 

In anonymous methods, if we define any parameters those can be accessed only within the anonymous method block but the anonymous methods can access the variables which we define in outer functions.

Visual Basic Anonymous Method Example

Following is the example of creating anonymous methods in visual basic using delegate keyword.

 

Module Module1

    ' Create delegate

    Public Delegate Sub MathOps(ByVal a As Integer)

    Sub Main(ByVal args As String())

        Dim y As Integer = 10

        ' Instantiate the delegate using anonymous method

        Dim ops As MathOps = Sub(ByVal x As Integer)

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

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

        End Sub

        ops(90)

        Console.ReadLine()

    End Sub

End Module

If you observe the above code, we created anonymous method, passing it as a delegate parameter and we are able to access outside variables within the anonymous method.

 

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

 

Add Result: 100

Subtract Result: 80

Visual Basic Send Anonymous Method as Parameter

In visual basic, we can pass an anonymous method as a parameter to the method that accepts delegate as a parameter.

 

Following is the example of sending an anonymous method as a parameter to another method in visual basic.

 

Module Module1

    ' Create delegate

    Public Delegate Sub MathOps(ByVal a As Integer)

    Sub Main(ByVal args As String())

        Dim y As Integer = 10

        ' Instantiate the delegate using an anonymous method

        Dim ops As MathOps = Sub(ByVal x As Integer)

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

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

    End Sub

        GetInfo(ops, 90)

        Console.ReadLine()

    End Sub

    Private Sub GetInfo(ByVal ops As MathOps, ByVal k As Integer)

        ops(k)

    End Sub

End Module

If you observe the above code, we created anonymous method and passing it as a parameter to the other method (GetInfo).

 

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

 

Add Result: 100

Subtract Result: 80

This is how we can send anonymous method as a parameter to another method in visual basic based on our requirements.

Visual Basic Anonymous Methods Drawbacks

Following are the some of the limitations of anonymous methods in visual basic.

 

  • In visual basic, we are not allowed to use jump statements such as goto, break and continue inside of an anonymous method.
  • Anonymous methods cannot access ref or out parameters of an outer scope.
  • The unsafe code cannot be accessed within the anonymous method.
  • Anonymous methods are not allowed on the left side of is operator.

Actually, the anonymous methods are introduced in earlier versions of visual basic to create methods without a name but in later versions, the lambda expressions have been introduced to achieve the better functionality than anonymous methods by writing inline code. To learn more about lambda expressions, check LINQ Lambda Expressions.

Visual Basic Anonymous Methods Overview

Following are the important points which we need to remember about the anonymous methods in visual basic.

 

  • Anonymous methods are the methods without a name. 
  • Anonymous methods can be defined by using delegate keyword and can be assigned to the variable of the delegate type.
  • Anonymous methods are useful to reduce the coding overhead in instantiating delegates because we don’t have to create a separate method.
  • In anonymous methods, if we define any parameters those can be accessed only within the anonymous method block.
  • We can pass anonymous methods as a parameter to the other methods.