Home >>VB.Net Built-in Functions >VB.Net Program of method overloading with type promotion

VB.Net Program of method overloading with type promotion

Write a VB.Net Program of method overloading with type promotion

Here, depending on the various number of arguments, we will overload the Add() method, here we name the overloaded method with type promotion, which means a method that accepts long argument, but instead of long we will pass integer argument.

Program :

The source code is provided below to demonstrate the overloading method with type promotion. The program given is compiled and successfully executed.

'VB.net program to demonstrate the method overloading with type promotion.


Module Module1
    Class Sample
        Public Sub Add(ByVal num1 As Integer, ByVal num2 As Long)
            Dim sum As Integer = 0
            sum = num1 + num2
            Console.WriteLine("Sum is: {0}", sum)
        End Sub
        Public Sub Add(ByVal num1 As Integer, ByVal num2 As Integer, ByVal num3 As Integer)
            Dim sum As Integer = 0
            sum = num1 + num2 + num3
            Console.WriteLine("Sum is: {0}", sum)
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample()
	 Dim num1 As Integer = 10
        Dim num2 As Integer = 20
        Dim num3 As Integer = 30
        obj.Add(num1, num2)
        obj.Add(num1, num2, num3)
    End Sub
End Module
	
Output:
Sum is: 30
Sum is: 60
Explanation:

We created a Module1 module in the program above. Here, to determine the sum of arguments, we created a Sample class that contains two Add() methods.

We have created the Add() method here for two and three arguments, applying the value of the arguments in both methods, and then printing the addition on the console screen.


No Sidebar ads