Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the method of overloading

VB.Net program to demonstrate the method of overloading

Write a VB.Net program to demonstrate the method of overloading

Here, to calculate the addition of a given number of arguments, we can create a sample class with two overloaded Add() methods.

Program :

The source code for displaying the overloading method based on the number of arguments is given below. The program given is compiled and successfully executed.

'VB.net program to demonstrate the method overloading

'based on the number 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 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()
        obj.Add(10, 20)
        obj.Add(10, 20, 30)
    End Sub
End Module
	
Output:
Sum is: 30
Sum is: 60
Explanation:

We created a Module1 module in the program above. 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.

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