Visual Basic .NET enables you to give a class more than one method with the same name but with different parameters. The program decides which version of the method to use based on the parameters being passed to the method.
For example, the Person class shown in the following code has two constructors named New. The first takes no parameters and initializes the object’s FirstName and LastName variables to default values. The second overloaded constructor takes two strings as parameters and uses them to initialize FirstName and LastName.
Public Class Person Public FirstName As String Public LastName As String Public Sub New() FirstName = "<first>" LastName = "<last>" End Sub Public Sub New(ByVal first_name As String, ByVal last_name As String) FirstName = first_name LastName = last_name End Sub End Class
The following code uses these constructors. The first statement passes no parameters to the constructor, so Visual Basic uses the first version of the New method. The second statement passes two strings to the constructor, so Visual Basic uses the second constructor.
Dim person1 As New Person() Dim person2 As New Person("Rod", "Stephens")
A common technique for providing constructors that take different numbers of arguments is to make the simpler constructors call those with more parameters. In the following code, the empty constructor calls a constructor that takes two parameters, passing it default values.
Public Class Person Public FirstName As String Public LastName As String Public Sub New() Me.New("<first>", "<last>") End Sub Public Sub New(ByVal first_name As String, ByVal last_name As String) FirstName = first_name LastName = last_name End Sub End Class
Two overloaded methods cannot differ only by optional parameters. For example, the first_name and last_name parameters in the previous constructor could not both be optional. If they were, then Visual Basic .NET could not tell which version of the New subroutine to call if the program passed it no parameters. Although you cannot make the parameters optional in the second constructor, you can get a similar result by combining the two constructors, as shown in the following code:
Public Class Person Public FirstName As String Public LastName As String Public Sub New( _ Optional ByVal first_name As String = "<first>", _ Optional ByVal last_name As String = "<last>") FirstName = first_name LastName = last_name End Sub End Class
Overloaded functions also cannot differ only in their return types. For example, you cannot have two versions of a function with the same name and parameters but different return types.
If you have Option Strict set to Off, there are many circumstances where Visual Basic performs automatic type conversion. In that case, it might not be able to decide which of two functions to use if they differ only in return type. For example, suppose that one version of the TotalCost function returns an Integer and another version returns a Double. If you set a string variable equal to the result of the function, Visual Basic wouldn’t know whether to use the Integer version or the Double version.