Showing posts with label Overloading. Show all posts
Showing posts with label Overloading. Show all posts

Sunday, February 6, 2011

Multiple Constructors

What are multiple constructors?
A constructor is also a method and so it may also be overloaded.

eg. Constructor with an empty parameter list
Public Class User
Public Sub New()
   ' Code...
End Sub

End Class

eg. Constructor containing 2 parameters in the parameter list
Public Class User
Public Sub New(UserName As String, Password As String)
   ' Code...
End Sub

End Class

eg. Constructor containing 3 parameters in the parameter list
Public Class User
Public Sub New(UserName As String, Password As String, MembershipType As Membership)
   ' Code...
End Sub

End Class

Method Overloading

What is Method Overloading?
Method Overloading is a means to having multiple methods with the same name, but which have differing parameter lists. Also called 'Polymorphism' - one name, many forms.

eg. getUserName function with empty parameter list
Public Function getUserName() As String
   ' Code...
Return Value
End Function

eg. getUserName function with a single string parameter called UserID
Public Function getUserName(UserID as Integer) As String
   ' Code...
Return Value
End Function

eg. getUserName function with a string parameter called UserID & a Date parameter called MembershipDate
Public Function getUserName(UserID As Integer, MembershipDate As Date) As String
   ' Code...
Return Value
End Function

Further reading:
Create Overloaded Methods in VB.NET http://www.devx.com/dotnet/Article/9303
Method Overloading http://en.wikipedia.org/wiki/Method_overloading