Home >>VB.Net Built-in Functions >VB.Net Program to Overload Not operator

VB.Net Program to Overload Not operator

Write a VB.Net Program to Overload Not operator

Here, using the operator method, we can overload the Not operator, change the Boolean True value to False or vice versa using the Not operator method.

Program :

The source code for the No Operator overload is given below. The program given is compiled and successfully executed.

'VB.net program to overload "Not" operator.


Class Sample
    Dim X As Boolean
    Sub SetX(ByVal val As Boolean)
        X = val
    End Sub
    Sub PrintX()
        Console.WriteLine("Value is: {0}", X)
    End Sub
    Public Shared Operator Not(ByVal S As Sample) As Boolean
        Dim temp As Boolean
        temp = Not S.X
        Return temp
    End Operator
End Class
Module Module1
    Sub Main()
        Dim obj As New Sample()
        Dim result As Boolean

        obj.SetX(True)
        obj.PrintX()

        result = Not obj
        Console.WriteLine("Result: {0}", result)

        obj.SetX(False)
        obj.PrintX()

        result = Not obj
        Console.WriteLine("Result: {0}", result)
    End Sub
End Module
	
Output:
Value is: True
Result: False
Value is: False
Result: True
Explanation:

We generated a Sample class in the above program, which includes two SetX() and PrintX() methods to set and print the values of the data members. Here, we have presented a method for overloading the Not 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 a Sample Class object and used the SetX() method to set the value of the data member and then use the No Operator with the Sample Class object and print the result on the console screen.


No Sidebar ads