devxlogo

Create Overloaded Methods in VB.NET

Create Overloaded Methods in VB.NET

ith VB.NET’s new method overloading feature, VB programmers don’t have to stay up all night trying to come up with different names for methods that basically do the same thing but differ in their argument lists. For example, you may create a method called GetPersonInfo() that could get a person’s information based on different arguments, such as last name, personid, and social security number. Method overloading allows you to create different logic for any of the given arguments using only GetPersonInfo() as the method name.

Until VB.NET offered method overloading, VB programmers had to mimic this feature with workarounds. For example, in the past I created a method that took in a list of optional arguments and then wrote code to identify which arguments to use and which logic to implement based on the arguments. Now, I don’t have to jump through hoops and neither do you.

This article demonstrates how to create overloaded methods in VB.NET, discusses some rules for creating overloaded methods, and describes the advantages to using this feature.

Creating an Overloaded Method
You can create an overloaded method in VB.NET in two ways:

  1. Simply create two or more methods that have different argument lists within the same class, for example:
        Public Function GetPersonInfo(ByVal v_sFirstName As String)    End Function    Public Function GetPersonInfo(ByVal v_lPersonId As Long) End Function

  2. Use the “Overloads” keyword, for example:
        Public Overloads Function GetPersonInfo(ByVal v_sFirstName As String)    End Function    Public Overloads Function GetPersonInfo(ByVal v_lPersonId As Long) End Function

    The keyword is optional. If you use it you’ll have to follow its rule. I’ll explain further in the next section, Rules for Creating Overloaded Methods.

That’s all there is to it. Now I want to discuss some rules that VB programmers must adhere to when creating an overloaded method.Arguments List Must Be Different
The arguments list must be different in overloaded methods. You can’t have two methods that each has an argument of the same data type but a different argument name. The VB.NET compiler will generate the following error: “Method [methodname] has multiple definitions with identical signatures” if the signatures are identical. Thus, VB.NET won’t allow the following within the same class:

    Public Overloads Function GetPersonInfo(ByVal v_sFirstName As String)    End Function    Public Overloads Function GetPersonInfo(ByVal v_sLastName As String)    End Function

However, the following is acceptable because the argument list is different:

    Public Overloads Function GetPersonInfo(ByVal v_sFirstName As String)    End Function    Public Overloads Function GetPersonInfo(ByVal v_sLastName As String, 
ByVal v_sFirstName As String) End Function

You Can’t Differentiate Between Overloaded Methods with Only an Optional Argument
Overloaded methods can’t differ by just an optional argument. For instance, the following code is not valid:

    Public Overloads Function GetPersonInfo(ByVal v_sFirstName As String)    End Function    Public Overloads Function GetPersonInfo(ByVal v_FirstName As String, 
Optional ByVal v_sLastName As String = "") End Function

You Can’t Differentiate Between Overloaded Methods with Only Return Types
Overloaded methods can’t differ by only their return types, such as the following:

    Public Overloads Function GetPersonInfo(ByVal v_FirstName As String) 
As String End Function Public Overloads Function GetPersonInfo(ByVal v_sFirstName As String)
As Data.DataSet End Function

Add Keyword to All Methods with the Same Method Name Within the Same Class
Because the “Overloads” keyword is optional (I prefer it because I’m not accustomed to overloaded methods yet), you have to add it to all subsequent methods with the same method name within the same class, such as the following:

Public Class Person    Public Overloads Function GetPersonInfo(ByVal v_sFirstName As String)    End Function    Public Overloads Function GetPersonInfo(ByVal v_lPersonId As Long)    End FunctionEnd Class

However, you can use the same method name in a different class and leave out the “Overloads” keyword. The reason you are allowed to do so is the method within a different class has a different signature. If you do leave out the “Overloads” keyword within a class in which you have used “Overloads” on one method and not on another (with the same method name), VB.NET will give you the following compilation error: “function (or sub)[methodname] must be declared ‘Overloads’ because another [methodname] is declared ‘Overloads’.”So why use overloaded methods at all? The following are the advantages that method overloading offers:

  • Method overloading gives a standard interface to the intent of the methods. A person calling the method has a clear understanding of what the method will do and what arguments it needs.
  • Overloaded methods work better with other languages that do not accept optional parameters (like C#, C++, and Java) to call the method.
  • The code is cleaner. You no longer need to clutter your code to figure out which logic to use based on the arguments.
  • The compiler will identify invalid arguments at compile time, helping to alleviate bugs associated to arguments during runtime.

How’d You Ever Get Along Without It?
After using method overloading for just a short time, I believe VB programmers will look back and wonder how they ever got along without it. Freed from the burden of figuring out how to create workarounds for overloading a method, VB programmers can devote more time to what they do best–developing business

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist