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 an anonymous method and passing it as a delegate parameter and assigned it 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.
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 an 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
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 an 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 an anonymous method as a parameter to another method in visual basic based on our requirements.
The following are some of the limitations of anonymous methods in visual basic.
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 better functionality than anonymous methods by writing inline code. To learn more about lambda expressions, check LINQ Lambda Expressions.
Following are the important points which we need to remember about the anonymous methods in visual basic.