VB.Net program to overload 'Mod' operator

Here, we are going to learn how to overload 'Mod' operator in VB.Net?
Submitted by Nidhi, on December 30, 2020 [Last updated : March 06, 2023]

Overloading 'Mod' operator in VB.Net

Here, we will overload the Mod operator with a class to apply mod operation between two objects to get the remainder.

Program/Source Code:

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

VB.Net code to overload 'Mod' operator

'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

Output:

Obj1:
        Num1: 10
        Num2: 30
Obj2:
        Num1: 3
        Num2: 7
Obj3:
        Num1: 1
        Num2: 2
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two methods SetValues(), PrintValues() to set and print the values of data members of the class. Here, we implemented one more method to overload the Mod operator.

After that, we created a module Module1 that contains the Main() method, the Main() method is the entry point for the program. Here, we created the two objects of the Sample class and then perform the Mod operation between two objects and return the remainder that will be assigned to the third object.

VB.Net Basic Programs »






Comments and Discussions!

Load comments ↻






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