SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

Code Generation

If you have an interface that you want to implement it can be time consuming to write all the class methods just to get your code to compile, especially if the interface has a large number of methods. SharpDevelop can save you time by generating this code for you.

SharpDevelop can generate code for the following:

  • Abstract base class methods
  • Constructors
  • Equals and GetHashCode methods
  • Property getters and setters
  • Interfaces
  • Overridable methods
  • Overridable properties
  • ToString methods

Whilst editing the code in the Text Editor select Auto code generation from the Tools menu, or use the keyboard shortcut Alt+Insert.

Auto code generation menu option

This opens the Code Generation dialog box.

Code generation dialog

Use the cursor keys to select Interface implementation, press the Tab key to move to the list of interfaces, press the Space Bar to select the ICollection interface, then press the Enter key to generate the code.

Generated interface implementation

Code Templates

SharpDevelop also has code templates that can be inserted into your code. To view the code templates, from the Tools menu select Options..., in the options dialog box expand the Coding category and select Code Templates.

Code templates

SharpDevelop ships with several templates. These templates can be customised or you can create new ones.

To use a template in your source code, move the cursor to where you want the code to be inserted, then type in the template name, for example tryf, then press the space bar to insert the code.

Try finally typed

Try finally code template inserted

Alternatively you can insert a code template using the code template dialog. Move the cursor to where you want the code to be inserted, then press Ctrl+J to open the Code Template dialog box.

Code template dialog

The code template can be selected directly and inserted or, just as with code completion, you can type in the first few characters of the template name and press the Tab key or Enter key to insert the template.

Surround With

The Code Template dialog box can also be used to surround selected code with a template. For example, if you wanted to put a try catch around some code, first select the code in the text editor.

Selected code to be surrounded with code template

Then press Ctrl+J to open the Code Template dialog box and then use the mouse or cursor keys to select the template.

Try catch template selected

Pressing the Enter or Tab key will insert the selected template.

Try catch template inserted

< previous | home | next >

Published Jul 05 2006, 11:50 AM by MattWard
Filed under:


No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this ( email address.