VB.Net program to overload the logical 'Or' operator

Here, we are going to learn how to overload the logical 'Or' operator in VB.Net?
Submitted by Nidhi, on January 03, 2021 [Last updated : March 06, 2023]

Overloading the logical 'Or' operator in VB.Net

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

Program/Source Code:

The source code to overload the logical "Or" operator is given below. The given program is compiled and executed successfully.

VB.Net code to overload the logical 'Or' operator

'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
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains that a data member val. we implemented SetVal() method to set the value of the data member. Here, we also implemented the Operator method to overload the logical "Or" operator.

After that, we created a module Module1 that contains the Main() method, the Main() method is the entry point for the program. And, we created the two objects of the Sample class and then perform the logical "Or" operation between two objects and return the Boolean value (True or False).

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.