ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Here, we are going to demonstrate the method overloading with type promotion in VB.Net.
Submitted by Nidhi, on December 25, 2020

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

Program/Source Code:

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

'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
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 an integer argument in place of long, it shows we can perform type promotion in method overloading.

VB.Net Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.