×

VB.Net Programs

VB.Net program to overload Like operator

By Nidhi Last Updated : November 15, 2024

Overloading Like operator in VB.Net

Here, we will overload Like operator using operator method to perform pattern matching with strings.

Program/Source Code:

The source code to overload Like operator is given below. The given program is compiled and executed successfully.

VB.Net code to overload Like operator

'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("Matched")
        Else
            Console.WriteLine("Not Matched")
        End If


        obj1.SetX("Hello")
        obj2.SetX("?illo")

        result = obj1 Like obj2
        If (result = True) Then
            Console.WriteLine("Matched")
        Else
            Console.WriteLine("Not Matched")
        End If
    End Sub
End Module

Output:

Matched
Not Matched
Press any key to continue . . .

Explanation:

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

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 two objects of Sample class and set the value of data member using SetX() method and then perform pattern matching between objects and print the result on the console screen.

VB.Net Basic Programs »


Comments and Discussions!

Load comments ↻





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