3

As the title says. From what I can see online the Overloads keyword is optional but is there ever a time when it is necessary? It even seems to be an error when used in a Module.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
James Hulse
  • 1,566
  • 15
  • 32
  • This [page](http://msdn.microsoft.com/en-us/library/8syxdc22(v=vs.90).aspx) on the MSDN lists keywords that cannot be used in a module. – Hornbydd Apr 03 '14 at 10:17
  • possible duplicate of ["Overloads" keyword in VB.NET](http://stackoverflow.com/questions/1173257/overloads-keyword-in-vb-net) – Sheridan Sep 16 '14 at 15:42

5 Answers5

6

No, it's not neccessary. You can overload methods and properties without the Overloads keyword.

However, if you use the Overloads or Overrides keyword on one overload of a method, you have to use it on all other overloads to that method in the class.

You can use the Overloads keyword instead of the Shadows keyword to shadow an inherited method with the same signature. Then you have to use either of the keywords, they are not both optional.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • See [Shadows vs Overloads in VB.NET](http://stackoverflow.com/questions/2515037/shadows-vs-overloads-in-vb-net) for more information. – Mark Hurd Mar 01 '12 at 02:35
  • Hard to believe this hasn't been corrected after all these years. 'Overload' is not always optional - if another method of the same name in the same class overrides a base class method, then 'Overloads' is necessary. – Dave Doknjas Jan 12 '16 at 20:09
  • 1
    @DaveDoknjas: Read the second paragraph. – Guffa Jan 13 '16 at 22:33
  • @Guffa: See my answer - even if you don't choose to use 'Overloads' on any method, the compiler will force you to add it in some circumstances - not optional. – Dave Doknjas Jan 13 '16 at 23:26
  • @DaveDoknjas: Yes, that's what it says in the second paragraph in my answer. – Guffa Jan 15 '16 at 23:49
2

There is only one case where the keyword Overloads is mandatory. If a method has the keyword Overloads then any new method of the same name within the type must also have Overloads

Other than that case, the keyword is optional.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

you don’t have to use the Overloads keyword to specify an overloaded method while within the same class. This is how C# handles overloading – there is no Overloads keyword in C#.
But using the Overloads keyword, tends to be more readable.
Check out this blog post for more detailed information.

Kamyar
  • 18,639
  • 9
  • 97
  • 171
0

In the interest of accuracy, I'm making this very late response since I noticed that the existing answers are simply incorrect. 'Overloads' is certainly required in a few cases, most commonly when another method of the same name overrides a base class method, as in the following example - the code simply will not compile if the 'Overloads' keyword is removed:

Public Class One
    Public Overridable Sub method()
    End Sub
End Class
Public Class Two
    Inherits One

    Public Overrides Sub method()
    End Sub

    Public Overloads Sub method(ByVal i As Integer)
    End Sub
End Class
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
0

Weirdly, there are times when you can't use the Overloads keyword. For example:

Module MyModule
    Overloads Sub MySub(Param1 As String)
    End Sub
    Overloads Sub MySub(Param1 As String, Param2 As Integer)
    End Sub
End Module

The compiler throws error, "Inappropriate use of 'Overloads' keyword in a module".

Not sure why, as you can still overload the method as usual.

SteveCinq
  • 1,920
  • 1
  • 17
  • 22