Home >>VB.Net Built-in Functions >VB.Net Program to Overload the Logical 'Xor' Operator

VB.Net Program to Overload the Logical 'Xor' Operator

Write a VB.Net Program to Overload the Logical 'Xor' Operator

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

Program :

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

'VB.net program to overload the logical "Xor" 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
        Dim result As Boolean
        result = (S1.val = True Xor S2.val = True)
        Return result
    End Operator
End Class
Module Module1
    Sub Main()
        Dim obj1 As New Sample()
        Dim obj2 As New Sample()
        obj1.SetValue(True)
        obj2.SetValue(True)
        If (obj1 And obj2) Then
            Console.WriteLine("True")
        Else
            Console.WriteLine("False")
        End If
        obj1.SetValue(False)
        obj2.SetValue(True)
        If (obj1 And obj2) Then
            Console.WriteLine("True")
        Else
            Console.WriteLine("False")
        End If
    End Sub
End Module
	
Output:
False
True
Explanation:

We created a class Sample in the above program that contains the val for a data member. To set the data member's value, we have implemented the SetVal() method. We have also added the Operator method here to overload the logical "Xor" operator.

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


No Sidebar ads