ARTICLE

Overloading in VB.NET

Posted by Satyapriya Nayak Articles | Visual Basic 2010 August 13, 2011
Overloading is a process of using several procedures with the same name but having different signatures.
Download Files:
 
Reader Level:

Overloading: It is a process of using several procedures with the same name but having different signatures. The signature changes in number of parameters, type of parameter, and order of parameter.

Program

Public Class Form1 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        raj()
        raj(2)
        raj(3, 3)
        raj("ravi", 1)
        raj(100, "rahul")
    End Sub

    Public Overloads Sub raj()
        MsgBox("Hello Raj")
    End Sub

    Public Overloads Sub raj(ByVal x As Integer)
        MsgBox(x * 5)
    End Sub

    Public Overloads Sub raj(ByVal x As Integer, ByVal y As Integer)
        MsgBox(x + y)
    End Sub

    Public Overloads Sub raj(ByVal s As String, ByVal x As Integer)
        MsgBox(s & ":has achieved rank" & x)
    End Sub

    Public Overloads Sub raj(ByVal x As Integer, ByVal s As String)
        MsgBox(s & ":has secured" & x)
    End Sub
End Class

Overriding: - It is the characteristics of the child or derived class, which overrides the characteristics of the parent class. The procedure in the base class, which could be overrides, is prefixed with the keyword Overridable. The procedure in the child class, which Overrides the base class procedure, is prefixed with the keyword overrides.

Mybase keyword is used in the child class to call the parent class procedure.

Program

Public Class Form2
    Dim a As New sagar1
    Dim b As New sagar2
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(a.amit(10, 20))
        MsgBox(b.amit(10, 20))
    End Sub

    Public Class sagar1
        Public Overridable Function amit(ByVal x As Integer, ByVal y As Integer)
            Dim z As Integer
            z = x + y
            Return z
        End Function
    End Class

    Public Class sagar2
        Inherits sagar1 
        Public Overrides Function amit(ByVal x As Integer, ByVal y As Integer)
            Dim z As Integer
            z = MyBase.amit(x, y) * 5
            Return z
        End Function

    End Class
End Class

Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor