Home >>VB.Net Built-in Functions >VB.Net program of method-overloading based on different arguments

VB.Net program of method-overloading based on different arguments

Write a VB.Net program of method-overloading based on different arguments

Here, to determine the addition of a given number of arguments depending on the various types of arguments, we can construct a sample class with two overloaded Add() methods.

Program :

The source code for demonstrate the overloading approach based on the various types of arguments is given below. The program given is compiled and successfully executed.

'VB.net program to demonstrate the method-overloading

'based on different types of arguments.


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 Integer, ByVal num2 As Double)
            Dim sum As Double = 0
            sum = num1 + num2
            Console.WriteLine("Sum is: {0}", sum)
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample()
        obj.Add(10, 20)
        obj.Add(10, 20.8)
    End Sub
End Module
	
Output:
Sum is: 30
Sum is: 30.8
Explanation:

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

The Main() method is the program entry point, we created a Sample class object here and then called all methods to print the addition of arguments on the console screen.


No Sidebar ads