Home »
VB.Net »
VB.Net Programs
VB.Net program to overload concatenates (&) operator
By Nidhi Last Updated : November 15, 2024
Overloading concatenates (&) operator in VB.Net
Here, we will overload the concatenate (&) operator using the operator method to concatenate two string objects.
Program/Source Code:
The source code to overload concatenates the (&) operator is given below. The given program is compiled and executed successfully.
VB.Net code to overload concatenates (&) operator
'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("Hello")
obj2.SetX("World")
result = obj1 & obj2
Console.WriteLine("Result: {0}", result)
End Sub
End Module
Output:
Result: HelloWorld
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. Here, we also implemented a method to overload the concatenates (&) operator to concatenate two 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 concatenate operation between objects and print the result on the console screen.
VB.Net Basic Programs »