tags:

views:

580

answers:

5

Do you really need this keyword to overload methods? What is the difference between using the overloads keyword vs. just having different method signatures?

+6  A: 

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.

Miky Dinescu
OP is asking about why to overload (vs having different names for methods), not why use overload keyword.
Adrian Godong
@Adrian Godong: OP is asking whether there is a need for the "Overloads" keyword in VB - not whether overloading is useful (as opposed to just using different method names)
Miky Dinescu
@Adrian: That's wrong - He asks about the keyword
Dario
Well, we'll let the OP clarify it then. :D
Adrian Godong
Hmm, I think he has now and he wrote: "Do you really need this keyword to overload methods? What is the difference between using the overloads keyword vs. just having different method signatures?" Ahh - Now it's clear
Dario
@Dario Hmm... after looking at Patrick's answer, I think you guys are right.
Adrian Godong
One `$entity`'s "expressive" is another `$entity`'s "excessively verbose." Not that this contributes anything, I'm just saying.
Adrien
A: 

The Overloads-Keyword is completely optional - I don't see advantages in using it.

Dario
OP is asking about why to overload (vs having different names for methods), not why use overload keyword.
Adrian Godong
That's wrong - He asks about the keyword: He doen't talk about names but signatures, we call that overloading
Dario
+1  A: 

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.

Meta-Knight
+3  A: 

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)
Patrick McDonald
A: 

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.

Armen S