Skip to main content

Promote to Parameter

  • 2 minutes to read

Removes all references to the field or a local declaration from the method, replacing it with a parameter. Calling code is now adjusted to pass in the field or expression of the local declaration as an argument for the new parameter.

Promote to Parameter 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

From the context menu or via shortcuts:

  • when the caret is on a local variable name within the variable declaration statement.

#Notes

  • Rename is automatically invoked for the newly declared parameter.
  • All method references are updated to pass the newly declared parameter’s value. By default, the right-hand side of the variable initialization expression is passed. The initialization expression is removed from the source method.

#Example

private int myField;
private string TestMethod()
{
    return String.Format("MyField value is {0}", myField);
}
private void CallTestMethod()
{
    string str = TestMethod();
}
Private myField As Integer
Private Function TestMethod() As String
    Return String.Format("MyField value is {0}", myField)
End Function
Private Sub CallTestMethod()
    Dim str As String = TestMethod()
End Sub

Result:

private int myField;
private string TestMethod(int myField)
{
    return String.Format("MyField value is {0}", myField);
}
private void CallTestMethod()
{
    string str = TestMethod(myField);
}
Private myField As Integer
Private Function TestMethod(ByVal myField As Integer) As String
    Return String.Format("MyField value is {0}", myField)
End Function
Private Sub CallTestMethod()
    Dim str As String = TestMethod(myField)
End Sub

#Screenshot

rsPromoteToParameter

See Also