Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Knowledge Base > Tutors' Corner > Overloading in VB.Net


Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2004, 08:07 PM
excaliber's Avatar
excaliber excaliber is offline
Senior Contributor

* Expert *
 
Join Date: Nov 2002
Location: Ohio, USA
Posts: 1,828
Post Overloading in VB.Net


Introduction
Overloading is a very nice feature that VB 6.0 lacked. It allows you to create multiple versions of the same method, each with a different argument list. In legacy VB, I would usually have to use ugly work arounds to cover the missing Overloading feature.

Method Signature
Each method has a so-called Signature that the compiler uses internally. By changing the arguments of a method, its signature changes as well. Because of this, VB.Net allows you to overload the method. This means you can have multiple versions of the method (all using the same name) as long as they have different method signatures (different argument lists). Overloading is actually pretty simple. Here's an example:

Code:
Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as string) txtOutput.text = "Name: " & strname End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as Int32) txtOutput.text = "Name: " & strname End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as string, ByVal strNickName as string) txtOutput.text = "Name: " & strname txtOutput.text += vbCrLf txtoutput.text += "Nickname: " & strNickName End function Public [COLOR=Blue]Overloads[/COLOR] Function Name() MsgBox("Nothing to see here") End function

There are four methods, all with identical names (which would normally be an error). But because they have different method signatures (ie. different argument lists for each), and the Overloads keyword was used, no error is shown. Here is a 'signature chart' of the methods (a list of each method and the argument types for the method. Will not error as long as there are not two identical signatures):

Code:
 Signature Chart
 Name(String)
 Name(Int32)
 Name(String, String)
 Name()
The overloaded methods can be used like this:

Code:
object.Name("Excaliber") object.Name(200) object.Name("Excaliber", "Pointy Sword of Might") object.Name()

Because both methods were overloaded, all of those calls work. It does this transparently, no need to specify which 'version' of the method to use. Notice how just changing the argument type changes the signature. The first and second version are identical except for the data type, but this is allowed (allowing you to build a set of methods to handle a variety of data types)

Things get fun when we start inheriting methods. Lets take a quick look. We have two classes, a base class and a derived class that inherits the base. In the base, we have two of the methods we used above. The derived class has the third.

Code:
Public [COLOR=Blue]Class[/COLOR] Person Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as string) txtOutput.text = "Name: " & strname End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as string, ByVal strNickName as string) txtOutput.text = "Name: " & strname txtOutput.text += vbCrLf txtoutput.text += "Nickname: " & strNickName End function End [COLOR=Blue]Class[/COLOR]
Code:
 Signature Chart
 Name(String)
 Name(String, String)
and
Code:
Public [COLOR=Blue]Class[/COLOR] Bob [COLOR=Blue]Inherits[/COLOR] Person Public [COLOR=Blue]Overloads[/COLOR] Function Name() MsgBox("Nothing to see here") End function End Class
Code:
  Signature Chart
  Name()
Notice how the derived class Bob has another method called "Name". Because it inherits Person, it also gets the other overloaded methods as well. This means that it's combined signature chart looks like this:
Code:
  Signature Chart
  Name(String)
  Name(String, String) 
 
  Name()
That means that once again, this code will work:

Code:
Dim object as Person object = New Bob object.Name("Excaliber") object.Name("Excaliber", "Pointy Sword of Might") object.Name()

Even though the methods were split up between different classes, because of inheritance and the keyword Overloads, you can safely do what you did within one class earlier. There are actually more things that you can't do with overloading than you can, so I will spend the rest of this article explaining those.


Things you can't do...

Big Note: Below are things that Will Not work. They are shown for purposes of describing what will and wont work. If you try them, it will error. Each has some code showing a bad way of doing it, and also the 'signature chart' showing why it wont work.

Firstly, you can't have two functions that are identical. This will throw an error. Since both are identical, they will both have identical method signatures, which is not allowed. An example of this is:

Code:
Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) 'code End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) 'different code End function
Code:
  Signature Chart
 Name(String)
 Name(String)

You also cant create methods that are differentiated by only an Optional parameter. Optional's do not change the method signature, so will also result in errors.

Code:
Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) 'code End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String, Optional ByVal strNickName as String = "") 'different code End function
Code:
  Signature Chart
  Name(String)
  Name(String)
On a similar note, changing only the argument name does not change the signature:

Code:
Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) 'code End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strSuperNameOfFear as String) 'different code End function
Code:
  Signature Chart
  Name(String)
  Name(String)
Next, overloaded methods can't be differentiated by only their return types either.

Code:
Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) as Boolean 'code End function Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) as String 'different code End function
Code:
  Signature Chart
  Name(String)
  Name(String)
The keyword Overloads is optional. But its highly recommended that its used, as its good programming practice. But, if you use it in one place, you have to use it in all methods of the same name (within the same class). For example, this will throw an error while compiling:

Code:
Public [COLOR=Blue]Overloads[/COLOR] Function Name(ByVal strName as String) 'code End function 'Would normally work, but the keyword Overloads was left off. 'This will cause a compile error, as it was used on the previous 'Either remove all of them, or make sure they all have the keyword Public Function Name(ByVal strName as String, ByVal strNickName as String) 'different code End function
Code:
  Signature Chart
  Name(String)
  Name(String, String)
 Signature chart is ok, but keyword Overloads was not used
Conclusion
This simple tutorial showed how to create and manipulate Overloaded methods, both in a single class and with inheritted classes. Overloading allows the developer to create cleaner, more efficient code that conforms to a standard. It provides a standard interface to accessing a method/property that would be ugly any other way.
__________________
RandomIRC - Your neighborhood's friendly IRC channel (irc.randomirc.com - #code)

"Perl - The only language that looks the same before and after RSA encryption."

Last edited by Iceplug; 04-06-2004 at 01:46 PM. Reason: Colorized for VB.NET
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing variables and controls using WinForms and VB.NET Bucky Tutors' Corner 0 02-24-2004 03:45 PM
VB6 vs VB.NET geo1st487 Tech Discussions 10 01-31-2004 09:37 PM
Open VB.NET 2002 project within VB.NET 2003 geo1st487 .NET General 2 01-10-2004 04:13 PM
VB.Net Office Integration FAQ Mike Rosenblum .NET Office Automation 2 12-19-2003 12:17 PM
A VB6 to VB.NET conversion sample Helmar General 0 11-14-2001 05:27 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->