Home > Code Generation, Coding Helpers > Coding Helpers – Auto Declare

Coding Helpers – Auto Declare

February 4th, 2011

Auto Declare allows you to quickly declare a variable (field, local or parameter) based on its type. To apply Auto Declare, type the class name and press the Ctrl+A shortcut. The variable will be declared with an appropriate name, and an action hint will be shown:

CodeRush Auto Declare

As you see, the identifier name is inherited from its type. The name of a variable is formatted according to your identifier code style settings. The style settings can be set for fields, local variables and parameters separately on the “Editor | Code Style | Identifiers” options page in the Options Dialog:

DXCore Identifiers options page

For example, the field of the StringBuilder type will be named _StringBuilder:

CodeRush Auto Declare

You can also specify the default names for identifiers of a certain class on the “Editor | Code Style | Default Names” options page:

DXCore Default Names options page

The Enabled option specifies whether the default names are used by Auto Declare. If the option is turned off, the declared identifier inherits the name from its type. Otherwise, if the Enabled option is turned on, AutoDeclare will use default names from this options page.

To add or delete a default name for a specific type, use the New and Delete buttons appropriately. For example, let’s specify the default name for the StringBuilder type to be just an “sb”.

  1. Click on the Enabled checkbox if it is not checked
  2. Click on the New button
  3. Fill the TypaName field
  4. Fill the Value field
  5. Click the OK button or press the Enter key to save your changes:

DXCore Adding new type on Default Names options page

Now, when we declare a local variable of the StringBuilder type, we will get the following result:

CodeRush Auto Declare

—–
Products: CodeRush Pro
Versions: 10.2 and up
VS IDEs: any
Updated: Feb/05/2011
ID: C065

Similar Posts: