How to overloading methods in Visual Basic .NET or in Visual Basic 2005 (311330)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311330

SUMMARY

This article provides sample code for overloading in Visual Basic .NET or in Visual Basic 2005. Overloading is the ability to define properties, methods, or procedures that have the same name but use different data types. Overloaded procedures appear as a single, versatile procedure but allow you to provide as many implementations as necessary to handle different kinds of data.

Overloading is especially useful when your object model dictates that you employ identical names for procedures that operate on different data types.

MORE INFORMATION

Overloading Constructors

Constructors provide initialization code when you instantiate objects in Visual Basic .NET or in Visual Basic 2005. If you overload the constructor of a Visual Basic object, you can alter this instantiation process. For example, if you overload the constructor of a class, you can initialize values for properties.

The following code demonstrates how to set the number of wheels on a vehicle class during instantiation:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Sedan As New Vehicle()
    MessageBox.Show (Sedan.NumberOfWheels.ToString)
    Dim TractorTrailer As New Vehicle(18)
    MessageBox.Show (TractorTrailer.NumberOfWheels.ToString)
End Sub

Public Class Vehicle
    Private m_Wheels As Integer
    Sub New()                                  'Default Constructor
        m_Wheels = 4
    End Sub
    Sub New(ByVal NumberOfWheels As Integer)   'Overloaded Constructor
        m_Wheels = NumberOfWheels
    End Sub
    Public Property NumberOfWheels()
        Get
            Return m_Wheels
        End Get
        Set(ByVal Value)
            m_Wheels = Value
        End Set
    End Property
End Class
				

Overloading Methods

Methods provide actions that the class can perform. If you overload a method, you can alter the action according to the parameters. The following code demonstrates how to increase the speed of a vehicle according to the parameter that is passed:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Sedan As New Vehicle()
    Sedan.IncreaseSpeed()
    Sedan.IncreaseSpeed (5)
End Sub

Public Class Vehicle
    Private m_VehicleSpeed As Integer
    Public Overloads Sub IncreaseSpeed()
        m_VehicleSpeed += 1
    End Sub
    Public Overloads Sub IncreaseSpeed(ByVal AmountToIncrease As Integer)
        m_VehicleSpeed += AmountToIncrease
    End Sub
End Class
				

Overloading Properties

Properties give characteristics to classes. The following code demonstrates how to set the number of wheels and the type of wheels that a vehicle has according to the parameter that is passed:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyVehicle As New Vehicle()
    MyVehicle.Wheels = 18
    MyVehicle.Wheels(Wheel.TypeOfWheel.Mag) = 4
End Sub

Public Class Vehicle
    Private m_NumOfWheels As Integer
    Private m_Wheel As New Wheel()
    Public Overloads WriteOnly Property Wheels()
        Set(ByVal Value)
            m_NumOfWheels = Value
            m_Wheel.WheelType = Wheel.TypeOfWheel.Standard
        End Set
    End Property
    Public Overloads WriteOnly Property Wheels(ByVal WheelType As Wheel.TypeOfWheel)
        Set(ByVal Value)
            m_NumOfWheels = Value
            m_Wheel.WheelType = WheelType
        End Set
    End Property
End Class

Public Class Wheel
    Public Enum TypeOfWheel
        Standard
        Mag
    End Enum
    Private m_Type As TypeOfWheel
    Public Property WheelType() As TypeOfWheel
        Get
            Return m_Type
        End Get
        Set(ByVal Value As TypeOfWheel)
            m_Type = Value
        End Set
    End Property
End Class
				

REFERENCES

For more information about overloading in Visual Basic .NET or in Visual Basic 2005, refer to the following topics in the Visual Studio .NET or Visual Studio 2005 Online Help documentation:
  • Rules of Procedure Overloading
  • Considerations in Overloading Procedures
  • Overload Resolution

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbinfo KB311330 kbAudDeveloper