Home »
VB.Net »
VB.Net Programs
VB.Net program to overload the logical 'And' operator
By Nidhi Last Updated : November 15, 2024
Overloading the logical 'And' operator in VB.Net
Here, we will create a class and implement a method to overload the logical "And" operator using the Operator method, and then perform logical "And" operation between objects.
Program/Source Code:
The source code to overload the logical "And" operator is given below. The given program is compiled and executed successfully.
VB.Net code to overload the logical 'And' operator
'VB.net program to overload the logical "And" 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
If (S1.val = True And S2.val = True) Then
Return True
Else
Return False
End If
End Operator
End Class
Module Module1
Sub Main()
Dim obj1 As New Sample()
Dim obj2 As New Sample()
obj1.SetValue(True)
obj2.SetValue(False)
If (obj1 And obj2) Then
Console.WriteLine("True")
Else
Console.WriteLine("False")
End If
obj1.SetValue(True)
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 "And" 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 'And' operation between two objects and return the Boolean value(True or False).
VB.Net Basic Programs »