×

VB.Net Programs

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

By Nidhi Last Updated : November 15, 2024

Overloading the logical 'Xor' operator in VB.Net

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

Program/Source Code:

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

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

'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
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 "Xor" 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 "Xor" 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.