ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

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.