What is the difference between creating an overloaded function as opposed to using optional parameters? Are there any advantages/disadvantages for using on way over another? Here's an example: This would be using optional parameters:
Public Sub LoadData(byVal value1 As String, optional byVal value2 As String = "")
This would be using overloads:
Public OverLoads Sub LoadData(byVal value1 As String)
Public OverLoads Sub LoadData(byVal value1 As String, byVal value2 As String)
They're identical, other than the fact that with an optional parameter, it must be last, and it must supply a default value. I understand, however, that overloaded methods are
generally preferable to optional parameters. Why? To keep each of your methods clear in purpose. That is, each method should do one thing well. As soon as you introduce optional parameters, you are diluting the cleanliness of that method, and introducing
branching logic that is probably best kept out of a method. This clarity of purpose becomes even more important when you start using inheritance. If you override a method that has one or more optional parameters, they become harder to work with. So, I'd suggest
that for anything other than quick and dirty classes, you use overloading in preference to optional parameters.
Another thing to note, is that if you use C# against those "optional" like methods, you have to supply the default values for every optional parameter. Microsofts standards say to use overloaded methods instead. The optional parameters were left in due to the
VB_ (supply your version here) programmers who requested it.
I wanted to know if its compulsory to use the Keyword Overloads ...
I was doing something very similar to this( method overloading )but never used the Keyword overloads yet got the same result. What exactly happens when you do not specify the keyword Overloads .
Public Sub LoadData(byVal value1 As String)
Public Sub LoadData(byVal value1 As String, byVal value2 As String)
I wanted to know if its compulsory to use the Keyword Overloads ...
As far as I have ever been able to tell, the Overloads keyword is not required. This keyword makes the overloading "explicit" where, if you simply define two or more methods with the same name, the overloading is "implicit".
My understanding is that the Overloads keyword is there to help the readability of the code. For example, say that you are working on a new class that inherits from an older class. In this new class you have a method with the same name as a method in the older
class. If the method looks like this:
Public Function PerformCalculation(value1 As Integer, value2 As Integer) As Integer
how would you know whether this method is overloading or shadowing (replacing) a method in the base class? We can use either the Overloads or the Shadows keyword to indicate what we are doing:
Public Function Overloads PerformCalculation(value1 As Integer, value2 As Integer) As Integer
is different from:
Public Function Shadows PerformCalculation(value1 As Integer, value2 As Integer) As Integer
which is different from:
Public Function Overrides PerformCalculation(value1 As Integer, value2 As Integer) As Integer
So that is my understanding: the Overloads keyword is not required, but helps readability.
rgcazo
Member
410 Points
94 Posts
Overloads vs optional parameters
Nov 06, 2003 11:26 AM|LINK
Applications Development Supervisor
SomeNewKid
All-Star
45894 Points
8027 Posts
Re: Overloads vs optional parameters
Nov 06, 2003 11:46 AM|LINK
mhawley
Participant
1314 Points
230 Posts
Re: Overloads vs optional parameters
Nov 06, 2003 12:42 PM|LINK
Lori
Member
5 Points
1 Post
Re: Overloads vs optional parameters
Dec 15, 2005 10:16 PM|LINK
Hi,
I wanted to know if its compulsory to use the Keyword Overloads ...
I was doing something very similar to this( method overloading )but never used the Keyword overloads yet got the same result. What exactly happens when you do not specify the keyword Overloads .
Public Sub LoadData(byVal value1 As String)
Public Sub LoadData(byVal value1 As String, byVal value2 As String)
Thanks!
SomeNewKid
All-Star
45894 Points
8027 Posts
Re: Overloads vs optional parameters
Dec 16, 2005 12:09 AM|LINK
My understanding is that the Overloads keyword is there to help the readability of the code. For example, say that you are working on a new class that inherits from an older class. In this new class you have a method with the same name as a method in the older class. If the method looks like this:
Public Function PerformCalculation(value1 As Integer, value2 As Integer) As Integer
how would you know whether this method is overloading or shadowing (replacing) a method in the base class? We can use either the Overloads or the Shadows keyword to indicate what we are doing:
Public Function Overloads PerformCalculation(value1 As Integer, value2 As Integer) As Integer
is different from:
Public Function Shadows PerformCalculation(value1 As Integer, value2 As Integer) As Integer
which is different from:
Public Function Overrides PerformCalculation(value1 As Integer, value2 As Integer) As Integer
So that is my understanding: the Overloads keyword is not required, but helps readability.
LudovicoVan
Star
9692 Points
1935 Posts
Re: Overloads vs optional parameters
Dec 16, 2005 10:57 AM|LINK
Maybe check this into local help:
Considerations in Overloading Procedures
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vbalr/html/a2001248-10d0-42c5-b0ce-eeedc987319f.htm
Sorry, i couldn't find an online version...
Regards. -LV