Home >>VB.Net Built-in Functions >VB.Net Program to overload the Logical 'Or' Operator

VB.Net Program to overload the Logical 'Or' Operator

Write a VB.Net Program to overload the Logical 'Or' Operator

Here, using the Operator method, we can create a class and implement a method to overload the logical "Or" operator and then execute a logical "Or" operation between objects.

Program :

Below is the source code for overload the logical 'Or' operator. The program given is compiled and successfully executed.

'VB.net program to overload the logical "Or" 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 Or 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
	
Output:
True
True
Explanation:

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. We have also implemented the Operator form here to overload the logical "Or" operator.

After that, we built a Module1 module that contains the Main() method, and the application entry point is the Main() method. And we generated the two Sample class objects and then performed the logical "Or" operation between the two objects and returned the Boolean value (True or False).