Create Overload
- 2 minutes to read
Creates an overload of a method allowing you to exclude specific parameters.
#Purpose
This refactoring automates a frequently performed task of creating a method overload that has a smaller parameter set. Having several method overloads with different parameter sets makes it possible to use the method by passing the same information in different formats or eliminating the necessity to specify optional information. These aspects result in increased method usability throughout the code.
#Availability
Available from the context menu or via shortcuts:
- when the edit cursor, or caret is on a method declaration.
#Notes
- When a new method is about to be declared, you should choose its position with a target picker.
- When a new method has been declared, the parameter selector is activated for it, so you can choose which parameters are to be excluded.
- Initially, the newly declared method has the same signature as the source method. The declared method’s body contains the code that calls the source method passing all the parameters.
- After the new method has been declared, you can navigate through its parameters using the TAB and SHIFT+TAB key combinations and exclude the required ones by pressing the SPACE key.
- When you exclude a parameter, Create Overload generates the code that declares a new variable with an empty value that will be passed to the source method instead of the removed parameter.
#Tips
- Create Overload works best if you first create a method that contains the maximum parameter set and then refine it by creating overloads with subsets of the originals parameter set.
#Example
public static int │AddValues(int x, int y)
{
return x + y;
}
Public Function │AddValues(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
Result:
public static int AddValues(int x)
{
int y = 0;
return AddValues(x, y);
}
public static int AddValues(int x, int y)
{
return x + y;
}
Public Function AddValues(ByVal x As Integer) As Integer
Dim lY As Integer = 0
Return AddValues(x, lY)
End Function
Public Function AddValues(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
#Animation
See Also