Home >>VB.Net Built-in Functions >VB.Net program to overload 'Mod' operator
Here, to get the remainder, we'll overload the Mod operator with a class to apply mod operation between two objects.
Program :The source code for Mod operator overload is given below. The program given is compiled and successfully executed.
'VB.net program to overload "mod" operator.
Class Sample
Dim num1 As Integer
Dim num2 As Integer
Sub SetValues(ByVal n1 As Integer, ByVal n2 As Integer)
num1 = n1
num2 = n2
End Sub
Public Shared Operator Mod(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
Dim temp As New Sample()
temp.num1 = S1.num1 Mod S2.num1
temp.num2 = S1.num2 Mod S2.num2
Return (temp)
End Operator
Sub PrintValues()
Console.WriteLine(vbTab & "Num1: {0}", num1)
Console.WriteLine(vbTab & "Num2: {0}", num2)
End Sub
End Class
Module Module1
Sub Main()
Dim obj1 As New Sample()
Dim obj2 As New Sample()
Dim obj3 As New Sample()
obj1.SetValues(10, 30)
obj2.SetValues(3, 7)
obj3 = obj1 Mod obj2
Console.WriteLine("Obj1: ")
obj1.PrintValues()
Console.WriteLine("Obj2: ")
obj2.PrintValues()
Console.WriteLine("Obj3: ")
obj3.PrintValues()
End Sub
End Module
We have created a Sample class in the above program, which includes two SetValues(), PrintValues() methods to set and print the values of the data members of the class. Here, one more method was implemented to overload the Mod operator.
After that, we built a Module1 module that contains the Main() method, and the program entry point is the Main() method. Here, the two objects of the Sample class are created and then the Mod operation is performed between two objects and the remaining object that is assigned to the third object is returned.