Home >>VB.Net Built-in Functions >VB.Net Program to Overload 'IsTrue' and 'IsFalse' Operators

VB.Net Program to Overload 'IsTrue' and 'IsFalse' Operators

Write a VB.Net Program to Overload 'IsTrue' and 'IsFalse' Operators

Here, using the Operator method, we create a class and enforce the methods for overload 'IsTrue' and 'IsTrue' operators and then checking boolean expressions using the object.

Program :

Below is the source code for overloading the 'IsTrue' and 'IsFalse' operators. The program given is compiled and successfully executed.

'VB.net program to overload "IsTrue" and "IsFalse" operator.


Class Sample
    Dim exp As Boolean
    Sub SetValue(ByVal e As Boolean)
        exp = e
    End Sub
    Public Shared Operator IsTrue(ByVal S As Sample) As Boolean
        Dim result As Boolean
        result = (S.exp)
        Return result
    End Operator
    Public Shared Operator IsFalse(ByVal S As Sample) As Boolean
        Dim result As Boolean
        result = (S.exp)
        If (result = False) Then
            Return True
        Else
            Return False
        End If
    End Operator
End Class
Module Module1
    Sub Main()
        Dim obj As New Sample()
        obj.SetValue(True And True)
        If (obj) Then
            Console.WriteLine("Expression is true")
        Else
            Console.WriteLine("Expression is false")
        End If
        obj.SetValue(True And False)
        If (obj) Then
            Console.WriteLine("true")
        Else
            Console.WriteLine("false")
        End If
    End Sub
End Module
	
Output:
true
false
Explanation:

In the above program, to store a condition expression, we created a Sample class that contains a data member exp. To set the value of the data members, we implemented the SetVal() method. Here, to overload the 'IsTrue' and 'IsFalse' operators, we have added two operator approaches.

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 Type object, and then set the Boolean Expression to the object, and if the object is used for conditions.