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.