Home >>VB.Net Built-in Functions >VB.Net Program to Overload Concatenates (&) Operator
Here, we can overload the concatenate (&) operator to concatenate two string objects using the operator method.
Program :The source code concatenates the operator (&) to overload is given below. The program given is compiled and successfully executed.
'VB.net program to overload concatenate "&" operator.
Class Sample
Dim X As String
Sub SetX(ByVal val As String)
X = val
End Sub
Public Shared Operator &(ByVal S1 As Sample, ByVal S2 As Sample) As String
Dim temp As String
temp = S1.X & 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 String
obj1.SetX("Hi")
obj2.SetX("Phptpoint")
result = obj1 & obj2
Console.WriteLine("Result: {0}", result)
End Sub
End Module
In the program above, we have created a Sample class that contains a SetX() method to set the value of members of the data. Here, a way of overloading the concatenate (&) operator to concatenate two strings was also implemented.
After that, we built a Module1 module that includes the Main() method. The entry point for the program is the Main() method. And, we generated two Sample Class objects and used the SetX() method to set the value of the data member, then execute a concatenated operation between objects and print the result on the console screen.