Back to Devexpress

Add Parameter

coderushforroslyn-115688-coding-assistance-code-providers-add-parameter.md

latest3.8 KB
Original Source

Add Parameter

  • Aug 03, 2020
  • 4 minutes to read

Purpose

Adds a new parameter to a method signature and update all method references.

Note

Add 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 cursor is in a method signature or in the arguments list in the method reference.

Usage

  1. Place the caret in a method signature.

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

  3. Select Add Parameter from the menu.

  4. Specify the type of new parameter and press Enter.

  5. For the base method and all overridden implementations, specify the parameter name. Use the Enter key to switch between method overrides.

  6. For each method reference, specify what is passed to the method as a newly added parameter. Use the Enter key to switch between method usages.

After execution, the Code Provider adds a new method parameter and updates all method usages.

csharp
static double Process(List<int> data, double shift) {
    // ...
    return data.Average();
}
static void Main() {
    var dataset1 = new List<int> { /* ... */};
    var dataset2 = new List<int> { /* ... */};

    var result1 = Process(dataset1, 2.4);
    var result2 = Process(dataset2, -1.8);
    // ...
}
vb
Function Process(data As List(Of Integer), shift As Double) As Double
    ' ...
    Return data.Average()
End Function
Sub Main()
    Dim dataset1 = New List(Of Integer) From { ... }
    Dim dataset2 = New List(Of Integer) From { ... }

    Dim result1 = Process(dataset1, 2.4)
    Dim result2 = Process(dataset2, -1.8)
    ' ...
End Sub

Note

Being called from the method arguments list, this Code Provider determines the parameter type automatically, according to the passed argument type.

Adding Parameters Passed by Reference

You can also specify the out or ref keyword if the parameter should be passed onto a method by reference. Consider the code snippet below.

csharp
public static double Process(List<int> data, double shift│){
    // ...
    return data.Average() + shift;
}
static void Main() {
    // ...

    var result1 = Process(dataset1, 2.4);
    // ...
    var result2 = Process(dataset2, -1.8);
    // ...
}
vb
Function Process(data As List(Of Integer), shift As Double) As Double
    ' ...
    Return data.Average() + shift
End Function
Sub Main()
    ' ...

    Dim result1 = Process(dataset1, 2.4)
    ' ...
    Dim result2 = Process(dataset2, -1.8)
    ' ...
End Sub

Follow the instructions from the Usage section to add a new parameter and specify the “ ref int “ string as a new parameter’s type (add ByRef in VB). If the out or ref keyword was specified, CodeRush passes the corresponding variable on method usages and declares/initializes it if required.

csharp
public static double Process(List<int> data, double shift, ref int stage) {
    // ...
    return data.Average() + shift;
}
static void Main() {
    // ...
    var stage = 0;

    var result1 = Process(dataset1, 2.4, ref stage);
    // ...
    var result2 = Process(dataset2, -1.8, ref stage);
    // ...
}
vb
Function Process(data As List(Of Integer),
    shift As Double, 
    ByRef stage As Integer) As Double
    ' ...
    Return data.Average() + shift
End Function
Sub Main()
    ' ...
    Dim stage = 0

    Dim result1 = Process(dataset1, 2.4, stage)
    ' ...
    Dim result2 = Process(dataset2, -1.8, stage)
    ' ...
End Sub