Promote to Parameter (optional)
- 2 minutes to read
In This Article
Removes all references to the local declaration from the method, replacing it with an optional parameter.
Promote to Parameter (optional) is a cascading refactoring. That is, the refactoring affects all method calls and method declarations in interfaces, base and descendant classes.
#Purpose
This refactoring makes a method much more flexible. Technically, it allows you to initialize its local variable in the calling statement. This extends the method’s application area.
#Availability
Available from the context menu or via shortcuts:
- when the caret is on a local variable name within the variable declaration statement.
#Example
public string TestMethod()
{
int │myVar = 15;
return String.Format("MyField value is {0}", myVar);
}
private void CallTestMethod()
{
string str = TestMethod();
}
Private Function TestMethod() As String
Dim │myInt As Integer = 15
Return String.Format("MyField value is {0}", myInt)
End Function
Private Sub CallTestMethod()
Dim str As String = TestMethod()
End Sub
Result:
public string TestMethod(int myVar = 15)
{
return String.Format("MyField value is {0}", myVar);
}
private void CallTestMethod()
{
string str = TestMethod(15);
}
Private Function TestMethod(Optional ByVal myInt As Integer = 15) As String
Return String.Format("MyField value is {0}", myInt)
End Function
Private Sub CallTestMethod()
Dim str As String = TestMethod(15)
End Sub