ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to learn how to overload the logical 'Xor' operator in VB.Net?
Submitted by Nidhi, on January 03, 2021

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 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 »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.