VB.Net program to overload exponential (^) operator

Here, we are going to learn how to overload exponential (^) operator in VB.Net?
Submitted by Nidhi, on January 13, 2021 [Last updated : March 06, 2023]

Overloading exponential (^) operator in VB.Net

Here, we will overload the exponential (^) operator using the operator method to calculate the power of a given number.

Program/Source Code:

The source code to overload the exponential (^) operator is given below. The given program is compiled and executed successfully.

VB.Net code to overload exponential (^) operator

'VB.net program to overload exponential "^" operator.

Class Sample
    Dim X As Integer
    Sub SetX(ByVal val As Integer)
        X = val
    End Sub
    Public Shared Operator ^(ByVal S1 As Sample, ByVal S2 As Sample) As Integer
        Dim temp As Integer
        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 Integer

        obj1.SetX(10)
        obj2.SetX(3)

        result = obj1 ^ obj2

        Console.WriteLine("Result: {0}", result)
    End Sub
End Module

Output:

Result: 1000
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 exponential (^) operator.

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 an exponential operation between objects.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





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