VB.Net program to overload arithmetic operators

Here, we are going to learn how to overload arithmetic operators in VB.Net?
Submitted by Nidhi, on January 02, 2021 [Last updated : March 06, 2023]

Overload arithmetic operators in VB.Net

Here, we will overload all arithmetic operations like plus (+), minus (-), multiplication (*), division (/), and remainder (Mod) using the operator keyword.

Program/Source Code:

The source code to overload arithmetic operators is given below. The given program is compiled and executed successfully.

VB.Net code to overload arithmetic operators

'VB.net program to overload arithmetic operators.

Class Sample
    Dim num As Integer
    Sub SetValues(ByVal n As Integer)
        num = n
    End Sub

    Public Shared Operator +(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num + S2.num
        Return (temp)
    End Operator

    Public Shared Operator -(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num - S2.num
        Return (temp)
    End Operator

    Public Shared Operator *(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num * S2.num
        Return (temp)
    End Operator

    Public Shared Operator \(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num \ S2.num
        Return (temp)
    End Operator

    Public Shared Operator Mod(ByVal S1 As Sample, ByVal S2 As Sample) As Sample
        Dim temp As New Sample()
        temp.num = S1.num Mod S2.num
        Return (temp)
    End Operator

    Sub PrintValues()
        Console.WriteLine(vbTab & "Num: {0}", num)
    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(7)
        obj2.SetValues(2)

        Console.WriteLine("Obj1: ")
        obj1.PrintValues()

        Console.WriteLine("Obj2: ")
        obj2.PrintValues()

        obj3 = obj1 + obj2
        Console.WriteLine("Addition: ")
        obj3.PrintValues()

        obj3 = obj1 - obj2
        Console.WriteLine("Subtraction: ")
        obj3.PrintValues()

        obj3 = obj1 * obj2
        Console.WriteLine("Multiplication: ")
        obj3.PrintValues()

        obj3 = obj1 \ obj2
        Console.WriteLine("Division: ")
        obj3.PrintValues()

        obj3 = obj1 Mod obj2
        Console.WriteLine("Remainder: ")
        obj3.PrintValues()
    End Sub
End Module

Output:

Obj1:
        Num: 7
Obj2:
        Num: 2
Addition:
        Num: 9
Subtraction:
        Num: 5
Multiplication:
        Num: 14
Division:
        Num: 3
Remainder:
        Num: 1
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 also implemented methods to overload all arithmetic operators using operator keywords.

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 the two objects of the Sample class and then perform all arithmetic operations using overloaded operators and then print the result on the console screen.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





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