Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create Overloaded Methods in VB.NET

VB.NETs method overloading allows VB programmers to develop different logic for methods that share the same name. Edward Cartagena shows how overloaded methods in VB.NET eliminates workarounds and gives VB programmers more time to develop business solutions.


advertisement
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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date