Skip to main content

Replace Temp with Query

  • 3 minutes to read

Replaces all references to a local variable with a call to a property or method that returns the variable’s initial value.

#Purpose

Replace Temp with Query is useful when you have a variable local to a method or property that you would like to make available from other methods or properties inside your class. It is also useful for reducing the number of local variables in your methods, which will reduce interdependencies when extracting methods, etc.

#Availability

Available from the context menu or via shortcuts:

  • when the edit cursor, or caret is on a local variable declaration. The variable should be initialized right in the declaration statement.

#Notes

  • Replace Temp with Query uses Extract Method or Extract Property to extract the code block used to initialize the variable. Extract Property has a higher priority so Extract Method is used only when Extract Property cannot be applied due to the nature of the assignment statement. Please refer to the Extract Method and Extract Property documentation for additional information on their availability.
  • You can choose the newly declared property’s or method’s location using the target picker (see Settings for Extract Method and Extract Property refactorings).
  • After the assignment code block has been extracted, Replace Temp with Query calls Inline Temp to replace all variable references with the extracted method or property call. Please refer to the Inline Temp documentation for more information on how it works.
  • If assignments to the local variable are found, then Replace Temp with Query replaces all references to the local variable and stops once it reaches an assignment to the local variable.

#Tips

  • If a variable’s declaration and initialization are on separate lines, you can always use Move Initialization to Declaration to make Replace Temp with Query available.
  • If the initialization of a local variable involves multiple statements, use Extract Property or Extract Method first to create a single-line initialization. After the initialization code has been extracted, use Inline Temp to replace the variable references with the extracted method or property calls.
  • If a variable has assignments other than initialization, you can use Split Temporary Variable before using Replace Temp with Query to provide an appropriate replacement.

#Example

private const int ConstA = 25;
private int TestMethod(int b)
{
    int a = ConstA;
    return a + b;
}
Private Const ConstA As Integer = 25
Private Function TestMethod(ByVal b As Integer) As Integer
    Dim a As Integer = ConstA
    Return a + b
End Function

Result:

private const int ConstA = 25;
private int PropertyA
{
    get
    {
        return ConstA;
    }
}
private int TestMethod(int b)
{
    return PropertyA + b;
}
Private Const ConstA As Integer = 25
Private ReadOnly Property PropertyA() As Integer
    Get
        Return ConstA
    End Get
End Property
Private Function TestMethod(ByVal b As Integer) As Integer
    Return PropertyA + b
End Function

#Animation

ReplaceTempWithQuery

See Also