Back to Devexpress

Declare Parameter

coderushforroslyn-117329-coding-assistance-code-providers-declaration-providers-declare-parameter.md

latest2.9 KB
Original Source

Declare Parameter

  • Aug 06, 2021
  • 3 minutes to read

Purpose

Generates a method parameter for an undeclared identifier and adds it to the method parameter list. This code provider also links the declared parameter and identifier.

You can also run this code provider inside a method to add the new parameter from the place where it is referenced.

Note

Declare Parameter is a cascading code provider. It affects all method calls and method declarations in interfaces and base and descendant classes.

Availability

Available when the caret is in an undeclared identifier name.

Usage

  1. Place the caret in the undeclared identifier (the “source” identifier in this example).

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

  3. Select Declare | Parameter from the menu and press Enter.

After execution, the code provider adds the new method parameter named after the identifier and a default value of the corresponding type to each method call.

csharp
public class Sample
{
    public static double ConvertToUSD(double amount, Currency source)
    {
        return amount / GetCurrencyRate(source);
    }

    protected static double GetCurrencyRate(Currency source)
    {
        throw new NotImplementedException();
    }

    static void Main(string[] args)
    {
        double price = ConvertToUSD(10, Currency.USD); // The first enum item is used.
    }
}

public enum Currency
{
    USD,
    EUR,
    GBR
}
vb
Public Class Sample
    Public Shared Function ConvertToUSD(ByVal amount As Double, ByVal source As Currency) As Double
        Return amount / GetCurrencyRate(source)
    End Function

    Protected Shared Function GetCurrencyRate(ByVal source As Currency) As Double
        Throw New NotImplementedException()
    End Function

    Private Shared Sub Main(ByVal args As String())
        Dim price As Double = ConvertToUSD(10, Currency.USD) ' The first enum item is used.
    End Sub
End Class

Public Enum Currency
    USD
    EUR
    GBR
End Enum

This code provider also links identifiers. If you change a linked identifier reference, CodeRush applies this change to other identifier references.

Blazor Support

The Declare Parameter code provider is available from the @code section of .razor files:

See Also

Declare Local

Make Implicit/Explicit

Promote to Parameter