VB.NET:- Over Loading
BCA -501
2
What is overloading?
 Overloading is the process of declaring methods of a class having the same name but
different signatures.
 The signature of a method comprises of the name of the method, the number and types
of arguments. The return type of method is not a part of the signature and hence cannot
be used as a factor for overloading.
 In VB.NET, functions, procedures, constructor and properties within a class can be
overloaded.
 The Overloads keyword is optional when overloading, but if any overloaded member
uses the Overloads keyword, then all other overloaded members with the same name
must also specify this keyword.
3
Example of Procedure Overloading
Module Module1
Public Class X
Sub Sum(a As Integer, b As Integer)
Dim c As Integer
c = a + b
Console.WriteLine("Sum of {0} and {1} is {2}", a, b, c)
End Sub
Sub Sum(a As String, b As String)
Dim c As String
c = a + b
Console.WriteLine("The concatenated string is " & c)
End Sub
End Class
Sub Main()
Dim Y As New X()
Y.Sum(10, 20)
Y.Sum("Hello", " World")
Console.ReadKey()
End Sub
End Module
4
Constructor Overloading
Module Module1
Public Class X
Public Sub New()
Console.WriteLine("Parameterless Constructor is called")
End Sub
Public Sub New(X As Integer)
Console.WriteLine("Integer parameter is passed through
Constructor")
Console.WriteLine("Value passed : {0}", X)
End Sub
Public Sub New(X As String)
Console.WriteLine("String parameter is passed through
Constructor")
Console.WriteLine("Value passed : {0}", X)
End Sub
End Class
Sub Main()
Dim x1 As New X()
Dim x2 As New X(10)
Dim x3 As New X("Programming")
Console.ReadKey()
End Sub
End Module
 Constructor Overloading is a special
case of method overloading.
 Parameterized Constructors that satisfy
the rule for overloading can be used
within classes.

Overloading

  • 1.
  • 2.
    2 What is overloading? Overloading is the process of declaring methods of a class having the same name but different signatures.  The signature of a method comprises of the name of the method, the number and types of arguments. The return type of method is not a part of the signature and hence cannot be used as a factor for overloading.  In VB.NET, functions, procedures, constructor and properties within a class can be overloaded.  The Overloads keyword is optional when overloading, but if any overloaded member uses the Overloads keyword, then all other overloaded members with the same name must also specify this keyword.
  • 3.
    3 Example of ProcedureOverloading Module Module1 Public Class X Sub Sum(a As Integer, b As Integer) Dim c As Integer c = a + b Console.WriteLine("Sum of {0} and {1} is {2}", a, b, c) End Sub Sub Sum(a As String, b As String) Dim c As String c = a + b Console.WriteLine("The concatenated string is " & c) End Sub End Class Sub Main() Dim Y As New X() Y.Sum(10, 20) Y.Sum("Hello", " World") Console.ReadKey() End Sub End Module
  • 4.
    4 Constructor Overloading Module Module1 PublicClass X Public Sub New() Console.WriteLine("Parameterless Constructor is called") End Sub Public Sub New(X As Integer) Console.WriteLine("Integer parameter is passed through Constructor") Console.WriteLine("Value passed : {0}", X) End Sub Public Sub New(X As String) Console.WriteLine("String parameter is passed through Constructor") Console.WriteLine("Value passed : {0}", X) End Sub End Class Sub Main() Dim x1 As New X() Dim x2 As New X(10) Dim x3 As New X("Programming") Console.ReadKey() End Sub End Module  Constructor Overloading is a special case of method overloading.  Parameterized Constructors that satisfy the rule for overloading can be used within classes.