id="info"

Developer

Explore an overloading method in VB.NET

Developers often need to define a method with the same name that may perform different functionalities. In order to do that, you should use the Overloads keyword in .NET.

Developers often need to define a method with the same name that may perform different functionalities. In order to do that, you should use the Overloads keyword in .NET. In this quick tip, I look at an example function that performs different sets of functionalities with the same name.

Overloads keyword

Overloading in general refers to creating multiple procedures with the same name that accept different argument types in a given class. You can use the Overloads keyword to declare a property or a method with the same name but with a different argument list.

Private Overloads Sub ShowDateTime(ByVal dDate As Date,

ByVal iOffset As Integer, ByVal sText As String)

     MessageBox.Show(dDate.AddDays(iOffset).ToString() & " " & sText)

End Sub

Private Overloads Sub ShowDateTime(ByVal dDate As Date)

     MessageBox.Show(dDate.ToString())

End Sub
 
Call ShowDateTime(Now)
Call ShowDateTime(Now, 5, "info")

In the example function, I use the Overloads keyword to define two procedures with different signatures. The first one accepts three parameters, and the second one accepts only one parameter. Depending on the number of arguments supplied in the call to the procedure, an appropriate procedure will be selected. Then I call these procedures one-by-one on the Form_Load event and get different results.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

Editor's Picks