VB.Net program to overload 'IsTrue' and 'IsFalse' operators

Here, we are going to learn how to overload 'IsTrue' and 'IsFalse' operators in VB.Net?
Submitted by Nidhi, on January 03, 2021 [Last updated : March 06, 2023]

Overloading 'IsTrue' and 'IsFalse' operators in VB.Net

Here, we will create a class and implement the methods to overload 'IsTrue' and 'IsTrue' operators using the Operator method, and then check boolean expressions using the object.

Program/Source Code:

The source code to overload 'IsTrue' and 'IsFalse' operators is given below. The given program is compiled and executed successfully.

VB.Net code to overload 'IsTrue' and 'IsFalse' operators

'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("Expression is true")
        Else
            Console.WriteLine("Expression is false")
        End If
    End Sub
End Module

Output:

Expression is true
Expression is false
Press any key to continue . . .

Explanation:

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

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 an object of the Sample class and then set the Boolean expression to the object and if conditions using the object.

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.