Someone asked me for this template today, so I figured I would just post it. This is a simple template that I use with Resharper to generate my fields and properties. This only applies when I am not using CodeSmith to automatically generate my business entity classes. But when I have to create fields and properties one at a time, I like to use this template.
private $TYPE$ $VARIABLE1$;
public $TYPE$ $VARIABLE2$
{
get { return $VARIABLE1$; }
set { $VARIABLE1$ = value; }
}
If you have not created Live Templates for Resharper before, go check it out in the Resharper | Options menu and select Live Templates.
Available In:
I set this setting to “in C# files where member declaration is allowed” so I can enter them at the class level.
Abbreviation & Name:
I assigned the abbreviation of “fp” to this template and called it “field property”.
$TYPE$
I wanted the type to be dynamic so I could enter string, int, or ArrayList (for example). And the type has to be used in both the field and in the property. So I used the same placeholder for both the field and the property. I set the macro to “none” . Its pretty generic and I don’t want it to default to a specific type, so that’s fine here.
$VARIABLE1$
The first instance of the variable name should be in lowercase. I will type this in myself. I don’t need a macro here but you can set the “suggest a name for variable” macro if you choose (or some other macro).
$VARIABLE2$
The second instance of the variable name should be the same as the first, except that the first letter should be uppercase. So I name the placeholders differently and I assign the macro “value of another variable with the first character in uppercase” to this placeholder and I assign $VARIABLE1$ as the variable that it refers to.
Running the Live Template
Once the live template is created, I type “fp” and hit the TAB key. This automatically generates the stub for the field and the property. It prompts me for the TYPE, which I entered string. Then it prompts me for the VARIABLE, for which I entered firstName. From these 2 entries it fills in the rest of the template and I end up with the following:
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
Disclaimer / Notes:
Use this at your own risk/pleasure … I do not warranty them J. Obviously you can change or modify the settings of Live Templates to generate simple or complex code logic.