Home >>VB.Net Built-in Functions >VB.Net Program to Overload Like Operator

VB.Net Program to Overload Like Operator

Write a VB.Net Program to Overload Like Operator

Here, using the operator method, we can overload Like Operator to perform pattern matching with strings.

Program :

The source code for Like Operator Overload is given below. The program given is compiled and successfully executed.

'VB.net program to overload Like operator.


Class Sample
    Dim X As String
    Sub SetX(ByVal val As String)
        X = val
    End Sub
    Public Shared Operator Like(ByVal S1 As Sample, ByVal S2 As Sample) As Boolean
        Dim temp As Boolean
        temp = S1.X Like S2.X
        Return temp
    End Operator
End Class
Module Module1
    Sub Main()
        Dim obj1 As New Sample()
        Dim obj2 As New Sample()
        Dim result As Boolean
        obj1.SetX("Hello")
        obj2.SetX("?ello")
        result = obj1 Like obj2
        If (result = True) Then
            Console.WriteLine("Correct")
        Else
            Console.WriteLine("Not Correct")
        End If
        obj1.SetX("Hello")
        obj2.SetX("?illo")
        result = obj1 Like obj2
        If (result = True) Then
            Console.WriteLine("Correct")
        Else
            Console.WriteLine("Not Correct")
        End If
    End Sub
End Module
	
Output:
Correct
Not Correct
Explanation:

In the above program, we have created a Sample class that contains a SetX() method to set the value of members of the data. And, we have also added a way to overload the operator of Like. To do pattern matching on strings, the Like operator is used.

After that, we built a Module1 module that contains the Main() method, and the program entry point is the Main() method. And, we generated two Sample Class objects and used the SetX() method to set the value of the data member and then perform pattern matching between objects and print the result on the console screen.


No Sidebar ads