Here, using the Operator method, we can create a class and implement a method to overload the logical 'And' operator, and then execute the logical 'And' operation between objects.Program :
Below is the source code for overload the logical 'And' operator. The program given is compiled and successfully executed.
'VB.net program to overload the logical "And" operator.
Class Sample Dim val As Boolean Sub SetValue(ByVal v As Boolean) val = v End Sub Public Shared Operator And(ByVal S1 As Sample, ByVal S2 As Sample) As Boolean If (S1.val = True And S2.val = True) Then Return True Else Return False End If End Operator End Class Module Module1 Sub Main() Dim obj1 As New Sample() Dim obj2 As New Sample() obj1.SetValue(True) obj2.SetValue(False) If (obj1 And obj2) Then Console.WriteLine("True") Else Console.WriteLine("False") End If obj1.SetValue(True) obj2.SetValue(True) If (obj1 And obj2) Then Console.WriteLine("True") Else Console.WriteLine("False") End If End Sub End Module
We created a class Sample in the above program that contains the val for a data member. We also introduced the SetVal() method to set the data member's value. Here, the operator method for overload the logical operator "And" was also implemented.
After that, we built a Module1 module that contains the Main() method, and the application entry point is the Main() method. And, we created the two Sample class objects and then performed the logical 'And' operation between two objects and returned the Boolean value (True or False).