VB.Net program to demonstrate the method overloading with type demotion

Here, we are going to demonstrate the method overloading with type demotion in VB.Net.
Submitted by Nidhi, on December 25, 2020 [Last updated : March 06, 2023]

Method overloading with type demotion in VB.Net

Here, we will overload the Add() method based on the different number of arguments, here we called an overloaded method with type demotion, it means a method that accepts integer argument but we will pass long argument in place of the integer.

Program/Source Code:

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

VB.Net code to demonstrate the example of method overloading with type demotion

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

Module Module1
    Class Sample
        Public Sub Add(ByVal num1 As Integer, ByVal num2 As Integer)
            Dim sum As Integer = 0

            sum = num1 + num2
            Console.WriteLine("Sum is: {0}", sum)
        End Sub

        Public Sub Add(ByVal num1 As Long, 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 Long = 15
        Dim num2 As Integer = 30
        Dim num3 As Integer = 45

        obj.Add(num1, num2)
        obj.Add(num1, num2, num3)
    End Sub
    
End Module

Output:

Sum is: 45
Sum is: 90
Press any key to continue . . .

Explanation:

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

Here, we created the Add() method with two and three arguments, in both methods we add the value of arguments and then print the addition on the console screen.

obj.Add(num1, num2)

In the above code, we passed a long argument in place of integer, which shows we can perform type demotion in method overloading.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





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