Visual Basic Predicate Delegate

In visual basic, Predicate is a built-in generic delegate and it is useful to validate whether the input parameter meets the specified condition or not and it’s same like Func and Action delegates to hold the reference of one or more methods.

 

The Predicate delegate can hold only the methods that take only one input parameter and returns a Boolean type value either true or false.

 

In latest versions, the Predicate delegate will available automatically with System namespace and it will accept only one input parameter and returns a Boolean value either True or False.

Visual Basic Predicate Delegate Syntax

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

 

Public Delegate Function Predicate(Of In T)(ByVal arg As T) AsBoolean

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

Visual Basic Predicate Delegate Example

Following is the example of defining the Predicate 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 dlgt As Predicate(Of Integer) = AddressOf IsGreaterthanZero

Dim result As Boolean = dlgt(10)

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

Console.ReadLine()

End Sub

Public Function IsGreaterthanZero(ByVal a As Integer) As Boolean

Dim i As Boolean = False

If a > 0 Then

i = True

End If

Return i

End Function

End Module

If you observe the above example, we created a Predicate delegate object (dlgt) with one input parameters (int) and assigned the method (IsGreaterthanZero) directly to the delegate object.

 

Here, IsGreaterthanZero method is accepting only one input parameter and validating whether the input parameter is greater than zero or not. In case, if it is greater than zero it will return the value true otherwise it will return false.

 

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

 

Result: True

Every time while creating the predicate delegate we must need to remember that we can include only one input parameter and return type must be Boolean.

Visual Basic Predicate Delegate with Anonymous Method

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

 

Sub Main(ByVal args As String())

Dim dlgt As Predicate(Of Integer) = Function(ByVal x As Integer)

Dim i As Boolean = False

If x > 0 Then

i = True

End If

Return i

End Function

Dim result As Boolean = dlgt(10) ' Result: True

End Sub

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

Visual Basic Predicate Delegate with Lambda Expressions

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

 

Sub Main(ByVal args As String())

Dim dlgt As Predicate(Of Integer) = Function(x)

Dim i As Boolean = False

If x > 0 Then

i = True

End If

Return i

End Function

Dim result As Boolean = dlgt(10) ' Result: True

End Sub

Visual Basic Predicate Delegate Overview

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

 

  • In visual basic, Predicate is a built-in generic delegate and it is useful to validate whether the input parameter meets the specified condition or not and it’s same like Func and Action delegates to hold the reference of one or more methods.
  • The Predicate delegate can hold only the methods that take only one input parameter and returns a Boolean type value either True or False.
  • While creating the predicate delegate we must need to remember that we can include only one input parameter and return type must be Boolean.
  • We can use Predicate delegate in anonymous methods and lambda expressions.