VB.net Program to Overload Less Than Equal to (=) Operators

You are currently viewing VB.net Program to Overload Less Than Equal to (=) Operators

VB.Net program to overload less than equal to (=) operators

'VB.net program to overload less than equal to (<=) and 
'greater than equal to (>=) operator.

Class Sample
    Dim num As Integer
    Sub SetValues(ByVal n As Integer)
        num = n
    End Sub

    Public Shared Operator <=(ByVal S1 As Sample, ByVal S2 As Sample) As Boolean
        If (S1.num <= S2.num) Then
            Return True
        Else
            Return False
        End If
    End Operator
    Public Shared Operator >=(ByVal S1 As Sample, ByVal S2 As Sample) As Boolean
        If (S1.num >= S2.num) Then
            Return True
        Else
            Return False
        End If
    End Operator

    Sub PrintValues()
        Console.WriteLine("Num: {0}", num)
    End Sub

End Class

Module Module1
    Sub Main()
        Dim obj1 As New Sample()
        Dim obj2 As New Sample()

        obj1.SetValues(10)
        obj2.SetValues(10)

        If (obj1 <= obj2) Then
            Console.WriteLine("Obj1 is less than equal to Obj2")
        Else
            Console.WriteLine("Obj1 is not less than qual to Obj2")
        End If

        obj1.SetValues(30)
        obj2.SetValues(20)

        If (obj1 >= obj2) Then
            Console.WriteLine("Obj1 is greater than equal to Obj2")
        Else
            Console.WriteLine("Obj1 is not greater than equal to Obj2")
        End If
    End Sub
End Module
Obj1 is less than equal to Obj2
Obj1 is greater than equal to Obj2
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply