Back to Devexpress

Promote to Parameter

coderushforroslyn-115557-coding-assistance-code-providers-promote-to-parameter.md

latest2.0 KB
Original Source

Promote to Parameter

  • Aug 03, 2020
  • 2 minutes to read

Purpose

Converts a local variable to the method parameter. Use this Code Provider inside a method to generalize it. Having a new parameter on a method will increase its flexibility for consumers.

Note

Promote to Parameter is a cascading Code Provider. That means that the Code Provider affects all method calls and method declarations in interfaces, base and descendant classes.

Availability

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

Usage

  1. Place the caret on the name of a local variable in its declaration.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Promote to Parameter from the menu.

After execution, the Code Provider removes the variable initialization, adds the new method parameter named after the variable and adds its initial value to each method call.

csharp
public static double EURToUSD(double amount, Currency source) {
    return amount / GetCurrencyRate(source);
}
static void Main(string[] args) {
    double price = EURToUSD(10, Currency.EUR);
}
vb
Public Function EURToUSD(amount As Double,
                         source As Currency) As Double
    Return amount / GetCurrencyRate(source)
End Function

Sub Main()
    Dim price As Double = EURToUSD(10, Currency.EUR)
End Sub

In the example above, the EURToUSD function has been generalized and can be renamed to ConvertToUSD.

See Also

Declare Parameter

Remove Unused Parameter

Widen Scope

Inline Temp