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.

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

  • Staff Microsoft showcases new NUIs

    TechFest, Microsoft's internal even took place this week with researchers showcasing some new interfaces the company is working on. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!