Home > Code Generation > Smart Constructor – Generating advanced type constructors in three keystrokes

Smart Constructor – Generating advanced type constructors in three keystrokes

April 19th, 2011

The Smart Constructor CodeRush feature allows you to add constructors to the current class or structure, and pass type members, like fields and properties for initialization through its parameters. Creating a constructor is as simple as just typing three keys: “cc & Space”:

CodeRush Smart Constructor preview

The Smart Constructor feature is based on the CodeRush Templates Engine, that’s why it is so simple to execute. The “cc” code template is specially dedicated to this feature, and the Space key simply expands the template, by default. The template mnemonic (cc) is easy to learn, when you know that it means the “Create Constructor” (cc).

If the active type has members, such as fields and/or auto-implemented properties, the Smart Constructor allows you to choose any of them for initialization inside the constructor, using the “Parameters to the constructor” dialog, for example:

CodeRush Smart Constructor and Choose fields dialog (CS)

Show Visual Basic code… »

CodeRush Smart Constructor and Choose fields dialog (VB)

You can use the mouse cursor or the Space key to check or uncheck the object in the list. When you are done with selecting members, press the Enter key to commit your changes or the Esc key, if you changed you mind on creating a new constructor. Those items that have a check will be initialized through parameters to the constructor when committed, for example:

CodeRush Smart Constructor with fields (CS)

Show Visual Basic code… »

CodeRush Smart Constructor with fields (VB)

By default, Smart Constructor generates an XML doc comment with a standard summary description, which is configurable, of course. All settings for the feature are available on the Editor | Text Commands | Smart Constructor options page in the IDE Tools Options Dialog:

CodeRush Smart Constructor options page

Here, on this page, you can specify the following options:

  • Whether to show the “Parameters to the constructor” dialog
    • Show always
    • Show only when there are more than [X] members, where you can choose X number
    • Never show
  • The sort mode inside the “Parameters to the constructor” dialog:
    • By declaration position will list member in the order as they appear in the code
    • By name
    • By type will sort member by its type (e.g. String, Int32, etc)
  • Whether to generate XML doc comment and its default summary description
  • Whether to generate default constructor for serializable types, for example:

CodeRush Smart Constructor and serializable constructor (CS)

Show Visual Basic code… »

CodeRush Smart Constructor and serializable constructor (VB)

  • Whether to generate read-only properties for the selected private fields, for example:

CodeRush Smart Constructor and properties (CS)

Show Visual Basic code… »

CodeRush Smart Constructor and properties (VB)

  • Whether public fields from the base type should be listed in the “Parameters to the constructor”, for example:

CodeRush Smart Constructor and base types fields (CS)

Show Visual Basic code… »

CodeRush Smart Constructor and base types fields (VB)

—–
Products: CodeRush Pro
Versions: 10.2 and up
VS IDEs: any
Updated: Apr/20/2011
ID: C088

Similar Posts: