Do you really need this keyword to overload methods? What is the difference between using the overloads keyword vs. just having different method signatures?
It was a design consideration. Of course it (VB) could have been designed to infer overloading by the function signature (like in C#) - so the Overloads keyword itself could have been omitted but in the end it's just in line with Visual Basic's expressiveness (which some consider overhead) and it was only a language design decision.
The Overloads
-Keyword is completely optional - I don't see advantages in using it.
Miky D is right. There is no difference between an overloaded method declared Overloads and another one which doesn't.
I just wanted to point out that the Overloads keyword is mandatory when another method with the same name is declared Overrides or Overloads. For example, if you override the Equals method like this:
Public Overrides Function Equals(ByVal obj As Object) As Boolean ...
Then you want to create an overload like this:
Public Function Equals(ByVal otherProduct As Product) As Boolean ...
You will get the following error:
"function 'Equals' must be declared 'Overloads' because another 'Equals'
is declared 'Overloads' or 'Overrides'."
You will get the same error if someone declared a method as Overloads, and you want to overload that method. You will have to either add the Overloads keyword to your method or remove it from the other method.
I personally never declare an overloaded method Overloads unless I don't have the choice, like in the situation above.
The Overloads keyword is optional, but if you use it for one method, you must use it for all overloads of that method.
' this is okay
Sub F1(s as String)
Sub F1(n as Integer)
' This is also okay
Overloads Sub F2(s as String)
Overloads Sub F2(n as Integer)
' Error
Overloads Sub F2(s as String)
Sub F2(n as Integer)
I ran into a situation where I was required to use the Overloads keyword.
I had a parent class, and an inherited class.
Public Class DataManager
Public MustOverride Function LoadDataSet(ByVal TableName as String, ByVal SQL as SQLObject) as DataSet
Public Function LoadDataSet(ByVal TableName as String, ByVal SQL as String) as DataSet
Return LoadDataSet(TableName, new SQLObject(SQL))
End Function
End Class
Public Class OracleDataManager
Inherits DataManager
Public Overloads Overrides Function LoadDataSet(ByVal TableName as String, ByVal SQLObj as SQLObject) as DataSet
// code here
End Function
End Class
In this example, I had to use the overloads keyword.