Replace with Local

Replaces the selected expression with a reference to a local variable which was previously assigned this value.

Purpose

Allows you to reduce code maintanence costs by eliminating duplicate values/expressions.

Availability

Available from the context menu or via shortcuts:

  • when the edit cursor, or caret is on a string or a number and there's a local variable initialized to the same string or number.
  • when an expression, previously assigned to a local variable, is selected.

Example

private string TestMethod(string str)
{
    string MyVariable = "Test string";
    return "Test string" + str;
}
Private Function TestMethod(ByVal str As String) As String
    Dim MyVariable As String = "Test string"
    Return "Test string" + str
End Function

Result:

private string TestMethod(string str)
{
    string MyVariable = "Test string";
    return MyVariable + str;
}
Private Function TestMethod(ByVal str As String) As String
    Dim MyVariable As String = "Test string"
    Return MyVariable + str
End Function

Screenshot

rsReplaceWithLocalCSharp

See Also