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 : Page 2

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
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 Function End 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'."



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